Detecting browser and OS via JavaScript running in the Internet zone
We're often asked how to detect the browser and OS versions via JavaScript running in the Intenet zone. Thanks to Jeremy, Forest, David, and a few other folks on my team for help with these samples. I previously posted a sample that parses the userAgent string and compares it to a finite set of regular expressions to map to a specific IE browser version (including IE8) and then hide content on the page that does not apply to the detected version. The sample below maps a finite set of specific OS and browser combinations to route users to a corresponding page. You will need to add parsing for "windows nt 6.1"
for Windows 7.
<script>
function PageInfo(contentCode, osName, osArchitecture, browserName, browserTest) {
this._contentCode = contentCode;
this._osName = osName;
this._osArchitecture = osArchitecture;
this._browserName = browserName;
this._browserTest = browserTest;
}
PageInfo.prototype = {
getContentCode: function() { return this._contentCode;},
getOsName: function() { return this._osName;},
getOsArchitecture: function(){ return this._osArchitecture;},
getBrowserName: function() { return this._browserName;},
browserTest: function(a) {
if(!this._browserTest)return false;
return this._browserTest.test(a);
}
}
function getUserAgentInfo(ua){
for(var k in PageInfoLookup){
if( PageInfoLookup[k].browserTest(ua)){
return PageInfoLookup[k];
}
}
return PageInfoLookup["FailOSIE"];
}
function AutoDetectNav(){
var o = getUserAgentInfo(navigator.userAgent);
return o;
}
var PageInfoLookup = new Object();
PageInfoLookup["2001"] = new PageInfo("2001","Windows Vista","x64","IE8",/^Mozilla\/4\.0.*MSIE 8\.\d+.*Windows NT 6\.0.*((Win64.*x64)|(WOW64))/);
PageInfoLookup["1001"] = new PageInfo("1001","Windows Vista","x64","IE7",/^Mozilla\/4\.0.*MSIE 7\.\d+.*Windows NT 6\.0.*((Win64.*x64)|(WOW64))/);
PageInfoLookup["1003"] = new PageInfo("1003","Windows Vista","x86","IE7",/^Mozilla\/4\.0.*MSIE 7\.\d+.*Windows NT 6\.0/);
PageInfoLookup["2000"] = new PageInfo("2000","Windows Vista","x86","IE8",/^Mozilla\/4\.0.*MSIE 8\.\d+.*Windows NT 6\.0/);
PageInfoLookup["2100"] = new PageInfo("2100","WinXP","x86","IE8",/^Mozilla\/4\.0.*MSIE 8\.\d+.*Windows NT 5\.1/);
PageInfoLookup["2101"] = new PageInfo("2101","WinXP|Win2k3","x64","IE8",/^Mozilla\/4\.0.*MSIE 8\.\d+.*Windows NT 5\.2.*(Win64.*x64)/);
PageInfoLookup["2102"] = new PageInfo("2102","WinXP|Win2k3","x64|IA64","IE8",/^Mozilla\/4\.0.*MSIE 8\.\d+.*Windows NT 5\.2.*WOW64/);
PageInfoLookup["2103"] = new PageInfo("2103","WinXP|Win2k3","IA64","IE8",/^Mozilla\/4\.0.*MSIE 8\.\d+.*Windows NT 5\.2.*Win64.*IA64/);
PageInfoLookup["0100"] = new PageInfo("0100","WinXP","x86","IE7",/^Mozilla\/4\.0.*MSIE 7\.\d+.*Windows NT 5\.1/);
PageInfoLookup["0010"] = new PageInfo("0010","winXP","x86","IE6",/^Mozilla\/4\.0.*MSIE 6\.\d+.*Windows NT 5\.1/);
PageInfoLookup["0101"] = new PageInfo("0101","WinXP|Win2k3","x64","IE7",/^Mozilla\/4\.0.*MSIE 7\.\d+.*Windows NT 5\.2.*(Win64.*x64)/);
PageInfoLookup["0102"] = new PageInfo("0102","WinXP|Win2k3","x64|IA64","IE7",/^Mozilla\/4\.0.*MSIE 7\.\d+.*Windows NT 5\.2.*WOW64/);
PageInfoLookup["0103"] = new PageInfo("0103","WinXP|Win2k3","IA64","IE7",/^Mozilla\/4\.0.*MSIE 7\.\d+.*Windows NT 5\.2.*Win64.*IA64/);
PageInfoLookup["0104"] = new PageInfo("0104","WinXP|Win2k3","x64","IE6",/^Mozilla\/4\.0.*MSIE 6\.\d+.*Windows NT 5\.2.*Win64.*x64/);
PageInfoLookup["0105"] = new PageInfo("0105","WinXP|Win2k3","x64|IA64","IE6",/^Mozilla\/4\.0.*MSIE 6\.\d+.*Windows NT 5\.2.*WOW64/);
PageInfoLookup["0106"] = new PageInfo("0106","WinXP|Win2k3","IA64","IE6",/^Mozilla\/4\.0.*MSIE 6\.\d+.*Windows NT 5\.2.*Win64.*IA64/);
PageInfoLookup["091"] = new PageInfo("091","WIn2k3","x86","IE6",/^Mozilla\/4\.0.*MSIE 6\.\d+.*Windows NT 5\.2/);
PageInfoLookup["092"] = new PageInfo("092","Win2k3","x86","IE7",/^Mozilla\/4\.0.*MSIE 7\.\d+.*Windows NT 5\.2/);
PageInfoLookup["093"] = new PageInfo("093","Win2k3","x86","IE8",/^Mozilla\/4\.0.*MSIE 8\.\d+.*Windows NT 5\.2/);
PageInfoLookup["0001"] = new PageInfo("0001","Win2k","x86","IE6",/^Mozilla\/4\.0.*MSIE 6\.\d+.*Windows NT 5\.0/);
PageInfoLookup["900"] = new PageInfo("900","Win2k","x86","IE5",/^Mozilla\/4\.0.*MSIE 5\.\d+.*Windows NT 5\.0/);
PageInfoLookup["090"] = new PageInfo("090","Win95|Win98|WinNT4","x86","",/^Mozilla\/4\.0.*MSIE \d+\.\d+.*Windows (NT 4\.0|(95|98|ME))/);
PageInfoLookup["FailOSIE"] = new PageInfo("FailOSIE","Unknown","Unknown","Unknown");
</script>