JS Quick Judgment IE browser (compatible with IE10 and IE11)

Source: Internet
Author: User

In many cases, we generally use navigator.useragent and regular expression to determine the version of IE browser, the following describes the different features in IE browser to determine the Internet Explorer

1 Judging IE browser and non-IE browser

The difference between IE browser and non-IE browser is that IE browser supports ActiveXObject, but non-IE browser does not support ActiveXObject. When the IE11 browser does not appear, we judge that IE and non-IE are often written like this

?
1 2 3 function isIe(){        return window.ActiveXObject ? true : false;   }

But in IE11, the return is false, and I tested it myself in the IE11 code below.

Alert (window. ActiveXObject); alert (typeof window. ActiveXObject);

The result is

What is this for? Clearly ActiveXObject is there, how to typeof the result is indeed undefined. Who knows the results tell me why is this? For God's horse?

Microsoft on the official website to say the IE11 activexobject difference. Http://msdn.microsoft.com/en-us/library/ie/dn423948%28v=vs.85%29.aspx. But there is no explanation for the typeof. If we use the code below to detect it, it's possible.

 in window)//returns false under Ie11

That's what I don't understand. The "ActiveXObject" in Window returns True, why was it that the code of IE was previously judged to return False in IE11? ask Daniel again for an explanation. Thank you
The following is a straightforward way to judge ie and non-IE browser compatible with IE11.

function Isie () {    return in window);}

Note that the prerequisite is that our program code does not overwrite the ActiveXObject, there should be no program to do so. Oh.

2 Judging IE6 Browser

Starting from IE7 IE is to support XMLHttpRequest objects, but IE6 is not supported. According to this feature and the previous judgment of IE's function isie () We know how to judge IE6. The judging method is as follows

function IsIe6 () {   ///  IE6 is not supported for window. XMLHttpRequest    return Isie () &&!  Window. XMLHttpRequest; }
3 Judging IE7 Browser

Because the document schema is supported starting from IE8, it supports Document.documentmode. IE7 is not supported, but IE7 is supported for XMLHttpRequest objects. The judging method is as follows

function IsIe7 () {   /// only ie8+ support Document.documentmode   return Isie () && Window. XMLHttpRequest &&! Document.documentmode;}
4 Judging IE8 Browser

Starting with IE9, Microsoft is slowly approaching the standard, we call IE678 a non-standard browser, ie9+ with other such as Chrome,firefox browser called Standard browser. The difference between the two is. Let's test the code below. What's the return?

Alert (-[1,]);//Nan is printed in IE678, but 1 is printed in standard browser

Then we can judge the IE8 browser according to the difference above. Methods are as follows

function IsIe8 () {   //  alert (!-[1,])//->ie678 returns nan so! Nan is true standard browser returns-1 so!-1 is false  return Isie () &&!-[1,]&&Document.documentmode ;}
5 Judging IE9, IE10, IE11 browser

From IE8 Browser is to support JSON built-in objects, starting from IE10 support JS Strict mode, about the strict mode of JS refer to this article http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html

ie9+ alert (!-[1,]) returns false,ie9+ is supported AddEventListener, but IE11 browser is not supported in the original IE unique event binding attachevent. According to these differences we can distinguish between IE9, IE10, IE11 browser.

6 Judging other browsers
/** * * from public class functions encapsulated in the past project * **///detection functionvarCheck =function(r) {returnr.test (Navigator.userAgent.toLowerCase ());};varStatics = {        /** * is the WebKit kernel browser*/Iswebkit:function() {          returnCheck (/webkit/); },        /** * is Firefox browser*/Isfirefox:function() {          returnCheck (/firefox/); },        /** * is Google Chrome*/Ischrome:function() {          return!statics.isopera () && Check (/chrome/); },        /** * is the Opera browser*/Isopera:function() {          returnCheck (/opr/); },        /** * Detects if it is a safari browser*/Issafari:function() {          //The Google Chrome browser also includes Safari          return!statics.ischrome () &&!statics.isopera () && Check (/safari/); }};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.