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..
        }
    }
}
 

No comments:

Post a Comment