My code is designed to detect IE4 or newer and is currently working flawlessly on websites for both my company's clients and my personal projects.
To implement this functionality, simply add the following constants and function variables into a JavaScript include file on your page...
//methods
var BrowserTypes = {
Unknown: 0,
FireFox: 1,
Chrome: 2,
Safari: 3,
IE: 4,
IE7: 5,
IE8: 6,
IE9: 7,
IE10: 8,
IE11: 8,
IE12: 8
};
var Browser = function () {
try {
//declarations
var type;
var version;
var sVersion;
//processing
switch (navigator.appName.toLowerCase()) {
case "microsoft internet explorer":
type = BrowserTypes.IE;
sVersion = navigator.appVersion.substring(navigator.appVersion.indexOf('MSIE') + 5, navigator.appVersion.length);
version = parseFloat(sVersion.split(";")[0]);
switch (parseInt(version)) {
case 7:
type = BrowserTypes.IE7;
break;
case 8:
type = BrowserTypes.IE8;
break;
case 9:
type = BrowserTypes.IE9;
break;
case 10:
type = BrowserTypes.IE10;
break;
case 11:
type = BrowserTypes.IE11;
break;
case 12:
type = BrowserTypes.IE12;
break;
}
break;
case "netscape":
if (navigator.userAgent.toLowerCase().indexOf("chrome") > -1) { type = BrowserTypes.Chrome; }
else { if (navigator.userAgent.toLowerCase().indexOf("firefox") > -1) { type = BrowserTypes.FireFox } };
break;
default:
type = BrowserTypes.Unknown;
break;
}
//returning result
return type;
} catch (ex) {
}
};
After adding these functions, you can easily use conditional statements like...
e.g.
value = (Browser() >= BrowserTypes.IE) ? node.text : node.textContent;
or
WindowWidth = (((Browser() >= BrowserTypes.IE9) || (Browser() < BrowserTypes.IE)) ? window.innerWidth : document.documentElement.clientWidth);
or
sJSON = (Browser() >= BrowserTypes.IE) ? xmlElement.text : xmlElement.textContent;
Understand how it works? I hope this information proves useful to you.
Also, remember to test the Browser() function post-IE10 release to ensure compatibility with any rule changes they might introduce.