Reputation: 1651
I am having huge issues in making my website work in multiple browsers. I like to detect when a user is using any IE version.
does anyone have coding to alert me when it detects IE?
Gordon
Upvotes: 1
Views: 1582
Reputation: 5075
$.browser.msie is not supported in latest version of jquery you can either add jquery-migrate-1.2.1.min.js or use following jquery function... for IE it also gives you version ...
call currentBrowser().browser to detect browser and currentBrowser().version for IE version .........
function currentBrowser() {
$.returnVal = "";
var browserUserAgent = navigator.userAgent;
if (browserUserAgent.indexOf("Firefox") > -1) {
$.returnVal = { browser: "Firefox" };
}
else if (browserUserAgent.indexOf("Chrome") > -1) {
$.returnVal = { browser: "Chrome" };
}
else if (browserUserAgent.indexOf("Safari") > -1) {
$.returnVal = { browser: "Safari" };
}
else if (browserUserAgent.indexOf("MSIE") > -1) {
var splitUserAgent = browserUserAgent.split(";");
for (var val in splitUserAgent) {
if (splitUserAgent[val].match("MSIE")) {
var IEVersion = parseInt(splitUserAgent[val].substr(5, splitUserAgent[val].length));
}
}
$.returnVal = { browser: "IE", version: IEVersion };
}
else if (browserUserAgent.indexOf("Opera") > -1) {
$.returnVal = { browser: "Opera" };
}
else {
$.returnVal =
{ browser: "other" };
}
return $.returnVal;
}
Upvotes: 0
Reputation: 23943
Putting aside value judgments of browser versus feature detection, you can also use IE conditional comments:
<script type="text/javascript">
var isIE = false;
</script>
<![if IE]> <!--- only runs in IE --->
<script type="text/javascript">isIE = true;</script>
<![endif]>
Upvotes: 1
Reputation: 1075885
Why do you want to detect IE? Feature detection, rather than browser sniffing, is the preferred way to deal with things whenever possible. (I don't think it's always possible, but it's almost always possible.) jQuery's jQuery.support
offers a number of these and the linked page for it offers great links to feature detection in general. Or, as Darin says, you can look at $.browser
, but that's really a last resort.
If you post questions with the actual problems you're having cross-browser, odds are pretty high people can help you address them in a way that doesn't rely on browser sniffing.
Upvotes: 1
Reputation: 236202
if( /msie/.test(navigator.userAgent.toLowerCase()) ) {
// omg it's an internet explorer
}
This is the real quick&dirty solution. Anyway you should do feature detection instead of browser detection. For instance, if you need a JSON
parser, go for
if( 'JSON' in window ) {}
This would be the correct solution. To check for a browser (maybe even the version also) is not realiable.
Upvotes: 1
Reputation: 1039548
You could use the $.browser
property:
if ($.browser.msie) {
alert('IE !');
}
Upvotes: 6