What is the simplest and best way to determine if the browser is IE or not? [duplicate]
(Don't care about the version. IE or not IE.)
http://api.jquery.com/jQuery.browser/
isIE = $.browser.msie;
$.browser
became deprecated in the meanwhile but is not yet deactivated. $.support
should be used instead. Example: $.support.boxModel
I wouldn't do it via browser sniffing (i.e. either directly or via a Javascript framework), because a User Agent string could easily be forged (and, in these cases, is depending on JavaScript, which could be turned off).
In this case (IE or not), I would use conditional comments in your HTML. They'll always work, whether JavaScript is enabled or not.
While this isn't the "simplest" way, here is a really good page to help with browser detection.
http://www.w3schools.com/js/js_browser.asp
Yay for the W3C.
<html>
<head>
</head>
<body>
<script type="text/javascript">
alert(navigator.appName);
</script>
</body>
</html>
This replies back with 'netscape' (for Firefox) or 'Microsoft Internet Explorer' (when I tried IE7 - not tried other platforms)
See the link below for more information , for instance.
http://www.javascriptkit.com/javatutors/navigator.shtml
And here's the same sort of thing with an 'if'/'else' structure.
<html>
<head>
</head>
<body>
<script type="text/javascript">
if (navigator.appName=="Microsoft Internet Explorer") {
alert("This is IE!");
}
else {
alert("This is not IE!");
}
</script>
</body>
</html>
Actually you should probably opt for the other post : stick to a well known library like jquery to sort out all the edge cases.
精彩评论