open new window popup using javascript


<script>
function popupW()

{ window.open(‘http://google.com&#8217;,’banco’,’toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no’); return true;

}

popupW();
</script>

Advertisements

Increment date by any number using js


This is the function to do so:

function increment_date(date,increment)
{
var dateString = date;
var myDate = new Date(dateString);
myDate.setDate(myDate.getDate() + increment);
var y = myDate.getFullYear(),
m = myDate.getMonth() + 1, // january is month 0 in javascript
d = myDate.getDate();
var pad = function(val) { var str = val.toString(); return (str.length < 2) ? “0” + str : str};
dateString = [y, pad(m), pad(d)].join(“/”);
return dateString;
}

 

and here is the example for that:

date_val = increment_date(‘2014/05/29’,parseInt(1));

 

Validate image with dimension


HTML CODE:

<input type=”file” required=”required” class=”file” id=”category_image” error=”error” name=”category_image”>

JAVA SCRIPT:

$(‘#CategoryEditForm’).validate({
errorElement: ‘div’,
errorClass: ‘help-block’,
focusInvalid: false,
rules: {
“category_image”: {
extension: “png|jpg|gif|jpeg|bmp”
}
},
messages: {
“category_image”: {
extension: “This field can accept images only!”
}
},
invalidHandler: function (event, validator) { //display error alert on form submit
$(‘.alert-danger’, $(‘.login-form’)).show();
},

highlight: function (e) {
$(e).closest(‘.form-group’).removeClass(‘has-info’).addClass(‘has-error’);
},

success: function (e) {
$(e).closest(‘.form-group’).removeClass(‘has-error’).addClass(‘has-info’);
$(e).remove();
},

errorPlacement: function (error, element) {
if(element.is(‘:checkbox’) || element.is(‘:radio’)) {
var controls = element.closest(‘div[class*=”col-“]’);
if(controls.find(‘:checkbox,:radio’).length > 1) controls.append(error);
else error.insertAfter(element.nextAll(‘.lbl:eq(0)’).eq(0));
}
else if(element.is(‘.select2’)) {
error.insertAfter(element.siblings(‘[class*=”select2-container”]:eq(0)’));
}
else if(element.is(‘.chosen-select’)) {
error.insertAfter(element.siblings(‘[class*=”chosen-container”]:eq(0)’));
}
else error.insertAfter(element.parent());
}
});
var _URL = window.URL || window.webkitURL;
$(‘.file’).on(‘change’, function() {
if($(this).valid()){
var file, img;
console.log(file);
console.log(this.files[0]);
if ((file = this.files[0])) {
img = new Image();
img.onload = function () {
alert(“Width:” + this.width + ”   Height: ” + this.height);
};
img.src = _URL.createObjectURL(file);
}
}
});

Get geolocation contry,city,region_name using js



    function getcurrentlocation()
    {
        theUrl="http://freegeoip.net/json/"+myIP();
        xmlhttp.open('get', theUrl, true);
        xmlhttp.onreadystatechange = ReplyUrl;
        xmlhttp.send(null);
    }

    function ReplyUrl()
    {
        if(xmlhttp.readyState == 4)
        {
            if(xmlhttp.responseText != "")
            {
                var response = xmlhttp.responseText;
                var parsedJSON = eval('('+response+')');
                var location;
                location = document.getElementById('location').value;
                console.log(parsedJSON.city+", "+parsedJSON.region_name+", "+parsedJSON.country_name);
                if(parsedJSON.city) location =location+","+parsedJSON.city;
                if(parsedJSON.region_name) location =location+","+parsedJSON.region_name;
                if(parsedJSON.country_name) location =location+","+parsedJSON.country_name;
                console.log(location);
                document.getElementById('location').value = location;
            }
            else
            {
                alert(xmlhttp.responseText);
            }
        }
    }
    function myIP()
    {
        if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
        else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

        xmlhttp.open("GET","http://api.hostip.info/get_html.php",false);
        xmlhttp.send();

        hostipInfo = xmlhttp.responseText.split("\n");

        for (i=0; hostipInfo.length >= i; i++) {
            ipAddress = hostipInfo[i].split(":");
            if ( ipAddress[0] == "IP" ) return ipAddress[1].trim();
        }

        return false;
    }

Use jQuery to get the width of an element in percent


Use jQuery to get the width of an element in percent or pixels, based on what the developer specified with CSS

var width = $('#someElt').width();
var parentWidth = $('#someElt').offsetParent().width();
var percent = 100*width/parentWidth;

or you can use

var width = ( 100 * parseFloat($('.largeField').css('width')) / parseFloat($('.largeField').parent().css('width')) ) + '%'