Friday, 20 December 2013

Generating a random number in JavaScript

 

Generating a Random Number in JavaScript in range 0 to 9

function randomString() {

            var chars = "0123456789";
            var string_length = 1;
            var randomstring = '';
            for (var i = 0; i < string_length; i++) {
                var rnum = Math.floor(Math.random() * chars.length);
                randomstring += chars.substring(rnum, rnum + 1);
            }
            alert(randomstring); //alert will show the random number
        } 

//setInterval call randomString() function in every 2 second

SB = setInterval(function() { randomString(); }, 2000);



Generating a Random Number in JavaScript by passing the minimum and maximum value

//Here you can generate random number my providing min and max value

function getRandomInt(min, max) {           
            var randomString = Math.floor(Math.random() * (max - min + 1)) + min;           
            alert(randomString);         
        }

Wednesday, 18 December 2013

How to detect IE browser and detect version in Javascript


Detect IE Browser in Javascript

This code detect the IE browser only.

Code: 

var isMSIE = /*@cc_on!@*/0;
if (isMSIE) 
{
   alert('ie brower');
}
else 
{
   alert('not ie browser’);
}
 

Detect IE Browser version in Javascript

This code detect the IE browser version.

Code: 

function getInternetExplorerVersion()

//Returns the version of Internet Explorer or a -1

//(indicating the use of another browser).

{

    var rv = -1; // Return value assumes failure.

    if (navigator.appName == 'Microsoft Internet Explorer') {

        var ua = navigator.userAgent;

        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");

        if (re.exec(ua) != null)

            rv = parseFloat(RegExp.$1);

    }

    return rv;

}


function checkVersion() {
   
    var ver = getInternetExplorerVersion();

    if (ver > -1) {
        if (ver == 6.0) {
            alert('IE version 6.0');
            //Your code here..
        }
        else if (ver == 7.0) {
            alert('IE version 7.0');
            //Your code here..
        }
        else if (ver == 8.0) {
            alert('IE version 8.0');
            //Your code here..
        }
        else {
            //Your code here..
        }
    }
}