简体   繁体   中英

Detecting any and all versions of Internet Explorer

There used to be a nice way to tell if a web browser is IE or not, by using this technique in HTML:

<!--[if IE]>
Non-IE browsers ignore this
<![endif]-->

or

<!--[if !IE]-->
IE ignores this
<!--[endif]-->

but this doesn't work anymore in IE 10.

Any idea what to use instead to tell IE from other web browsers (using HTML or JavaScript)?

PS. I need to be able to tell ANY version of IE from non-IE web browser.

I appreciate all your insight, but none of it answers my actual question. Again, I am not asking about the feature detection . All I need to know is if the web browser is IE or not. The following uses JavaScript and seems to work for all current versions of IE (including IE10):

<![if IE]>
<script type='text/javascript'>
if(/*@cc_on!@*/false)
var bIsIE = 1;
</script>
<![endif]>

and then to check if it's IE, do this from JavaScript:

if (typeof (bIsIE) != 'undefined')
{
    //The web browser is IE
}
else
{
    //The web browser is not IE
}

Obviously the code above assumes that the web browser has JavaScript enabled. But in my case the browser detection is relevant only if it has scripts enabled.

Every version of Internet Explorer is different from the others, just as every version of Chrome, Firefox, and Opera are different from their predecessors. You don't target vendors such as "Microsoft", "Google", or "Mozilla" when you develop websites—you target features.

Rather than asking " I'd like to use ::after, is this browser a Microsoft browser? " You should instead ask " Does this browser support pseudo-elements on the :: prefix? " This is feature-detection, and it's nearly always perfectly on target. Rather than guessing what a browser is capable of by its vendor, you determine what it's capable of by what it can actually do.

This may not be the answer you were looking for, but it's the correct answer nonetheless. If you're asking how to identify all Microsoft browsers, you are approaching the problem (or what you perceive to be a problem) incorrectly.

For proper solutions, I would encourage you to use tools like jQuery and Modernizr . These will handle API normalization, shimming of newer elements in older browsers, as well as feature-detection. This is the correct way to do things, and had developers been taking this approach from the beginning you may not have such a distaste for Internet Explorer today.

The link you give in your question - doesn't work anymore - which is to Windows Internet Explorer Engineering Team Blog leads to the following statement

Conditional Comments

 <!--[if IE]> This content is ignored in IE10 and other browsers. In older versions of IE it renders as part of the page. <![endif]--> 

This means conditional comments can still be used, but will only target older versions of IE. If you need to distinguish between more recent browsers, use feature detection instead.

It seems to me that the IE team are strongly pushing for the use of feature detection rather than browser detection as the quote from the feature detection link above shows.

Same Markup: Core Guidelines

 **DO** Feature Detection Test whether a browser supports a feature before using it. Behavior Detection Test for known issues before applying a workaround. **DON'T** Detect Specific Browsers Also known as browser detection. Don't use the identity of a browser (eg navigator.userAgent) to alter page behavior. Assume Unrelated Features Don't perform feature detection for one feature, and then proceed to use a different feature. 

So it appears that the Windows Internet Explorer Engineering Team are setting IE up so that you will not be able to use browser detection for IE10 and above.

EDIT I do not use IE10 but does

navigator.appName=='Microsoft Internet Explorer';

work in IE10?

It isn't enough to just say IE10 is good enough and ignore the problem. It really depends on what you are trying to do. For most purposes feature detection would likely handle what you need. The far, far more complicated route is to start user agent detection by pulling in the user agent string from the HTTP request header. If you aren't careful with this you can go wrong pretty quickly.

To view your current user agent string in a browser JS console:

console.log(navigator.userAgent);

Here is a list of reported user agent strings across all kinds of browsers:

http://www.zytrax.com/tech/web/browser_ids.htm

Note that all MS Explorer agent strings will contain "MSIE," but first you have to weed out browsers like Opera that will also include the "MSIE" string in some cases.

This function returns true if the client browser is Internet Explorer , tested on versions 9-10-11 .

function isIE(v) {
    var ie;
    ie = RegExp('msie' + (!isNaN(v)?('\\s'+v):''), 'i').test(navigator.userAgent);
    if (!ie) { ie = !!navigator.userAgent.match(/Trident.*rv[ :]*11\./) }
    return ie;
}

// Example
var ie = isIE();
var ie9 = isIE(9);
var ie10 = isIE(10);

NOTE: the function is incomplete and won't allow for isIE(11)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM