Reputation: 2285
I've using the below function to detect my IE version:
function isIE()
{
var myNav = navigator.userAgent.toLowerCase();
return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
}
This code will return IE version if IE browser is used and it will return false if other browser is used. It can be use in IE6, IE9 and Chrome. When I try on Firefox latest version v20, it doesn't work (website hang). Do you guys know why?
Or is there any other function that can be use to detect IE version?
Upvotes: 0
Views: 1080
Reputation: 138
Got this script from msdn:
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
// http://msdn.microsoft.com/en-us/library/ms537509(v=vs.85).aspx
{
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;
}
Personally, I always just apply a class to <html>
, using conditional comments. Later on, i check the classname in javascript.
Upvotes: 0
Reputation: 33972
This works for me, I've tested it on IE9 through IE11. The useragent string in IE11 is different than the versions before it, which is why there are two different tests here.
if (/msie|trident/ig.test(navigator.userAgent)) {
var matches = navigator.userAgent.match(/MSIE\s([\d\.]+)/) || navigator.userAgent.match(/rv:([\d\.]+)/);
var ieVersion = parseInt(matches[1], 10);
}
Upvotes: 0
Reputation: 63
you can use regular expression..
<script type="text/javascript">
var userAgent = navigator.userAgent;
var internet_version = -1;
userAgent.replace(/MSIE .{1,5};/g, function(findS) {
internet_version = parseFloat(findS.substring(5, findS.length-1));
});
alert(internet_version);
</script>
Upvotes: 0
Reputation: 11238
This is pretty ugly but straightforward, using IE conditional comments.
Edit: here's a better version based on wikipedia's example
<script>
var internet_explorer_version = -1;
/*@cc_on
@if (@_jscript_version == 10)
internet_explorer_version = 10;
@elif (@_jscript_version == 9)
internet_explorer_version = 9;
@elif (@_jscript_version == 5.8)
internet_explorer_version = 8;
@elif (@_jscript_version == 5.7 && window.XMLHttpRequest)
internet_explorer_version = 7;
@elif (@_jscript_version == 5.6 || (@_jscript_version == 5.7 && !window.XMLHttpRequest))
internet_explorer_version = 6;
@elif (@_jscript_version == 5.5)
internet_explorer_version = 5.5;
@end
@*/
</script>
Conditional comments are only supported by IE4+. Older IE or non-IE browsers will skip all the checks and leave you with -1
. This script checks for 5.5+, which should be plenty far back for anything you need.
Upvotes: 0
Reputation: 4021
/**
* Detects browser endgine and browser version.
* @class BrowserDetect
* @property {string} browser browser engine
* @property {number} version browser version
* @return {object}
*/
var BrowserDetect = {
init: function() {
this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
this.version = this.searchVersion(navigator.userAgent)
|| this.searchVersion(navigator.appVersion)
|| "an unknown version";
this.OS = this.searchString(this.dataOS) || "an unknown OS";
},
searchString: function(data) {
for (var i = 0; i < data.length; i++) {
var dataString = data[i].string;
var dataProp = data[i].prop;
this.versionSearchString = data[i].versionSearch || data[i].identity;
if (dataString) {
if (dataString.indexOf(data[i].subString) !== -1)
return data[i].identity;
}
else if (dataProp)
return data[i].identity;
}
},
searchVersion: function(dataString) {
var index = dataString.indexOf(this.versionSearchString);
if (index === -1)
return;
return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
},
dataBrowser: [
{
string: navigator.userAgent,
subString: "Chrome",
identity: "Chrome"
},
{string: navigator.userAgent,
subString: "OmniWeb",
versionSearch: "OmniWeb/",
identity: "OmniWeb"
},
{
string: navigator.vendor,
subString: "Apple",
identity: "Safari",
versionSearch: "Version"
},
{
prop: window.opera,
identity: "Opera",
versionSearch: "Version"
},
{
string: navigator.vendor,
subString: "iCab",
identity: "iCab"
},
{
string: navigator.vendor,
subString: "KDE",
identity: "Konqueror"
},
{
string: navigator.userAgent,
subString: "Firefox",
identity: "Firefox"
},
{
string: navigator.vendor,
subString: "Camino",
identity: "Camino"
},
{// for newer Netscapes (6+)
string: navigator.userAgent,
subString: "Netscape",
identity: "Netscape"
},
{
string: navigator.userAgent,
subString: "MSIE",
identity: "Explorer",
versionSearch: "MSIE"
},
{
string: navigator.userAgent,
subString: "Gecko",
identity: "Mozilla",
versionSearch: "rv"
},
{// for older Netscapes (4-)
string: navigator.userAgent,
subString: "Mozilla",
identity: "Netscape",
versionSearch: "Mozilla"
}
],
dataOS: [
{
string: navigator.platform,
subString: "Win",
identity: "Windows"
},
{
string: navigator.platform,
subString: "Mac",
identity: "Mac"
},
{
string: navigator.userAgent,
subString: "iPhone",
identity: "iPhone/iPod"
},
{
string: navigator.platform,
subString: "Linux",
identity: "Linux"
}
]
};
Upvotes: 0
Reputation: 6667
Use this function it works.
function checkVersion() {
var msg = "You're not using Internet Explorer.";
var ver = getInternetExplorerVersion();
if (ver > -1) {
if (ver >= 8.0)
msg = "You're using a recent copy of Internet Explorer."
else
msg = "You should upgrade your copy of Internet Explorer.";
}
alert(msg+" version ->"+ver);
}
Upvotes: 0
Reputation: 2399
I use this
public static class DeviceHelper
{
public static bool IsMobile(string userAgent)
{
userAgent = userAgent.ToLower();
var currentSubContent = NodeLocator.GetNodeOfExactType<Language>();
var isMobileDevicesActivatedInBackend = currentSubContent.ActiveForMobileDevices;
if (isMobileDevicesActivatedInBackend)
{
return userAgent.Contains("iphone") |
userAgent.Contains("ppc") |
userAgent.Contains("windows ce") |
userAgent.Contains("blackberry") |
userAgent.Contains("opera mini") |
//userAgent.Contains("mobile") |
userAgent.Contains("palm") |
userAgent.Contains("portable") |
(userAgent.Contains("android") && userAgent.Contains("mobile")) |
(userAgent.Contains("windows") && userAgent.Contains("mobile"));
}
else
{
return false;
}
}
}
Then you can just add what you need from here: to detect version of MSIE
or in jquery just use their api:
if ($.browser.msie){
}
Upvotes: 0
Reputation: 276161
JQuery has $.browser : http://api.jquery.com/jQuery.browser/
you can do :
if ($.browser.msie){
}
Upvotes: 1