Recommended reading JS fast judge IE browser (compatible IE10 and IE11) _javascript skills

Source: Internet
Author: User

To determine whether IE browser with window.navigator.userAgent, tracking this information, found in the development environment, identified as IE10, but the access server is identified as IE11, but IE11 useragent is not MSIE logo, the reason is this.


The way to judge IE browser is to change the following.

function Isie () {//ie?
 if (!! Window. ActiveXObject | | ' ActiveXObject ' in Window ' return
 true;
 else return
 false;
 

Here are some sharing, you can see, very practical analysis of the explanation

In many cases, we generally use navigator.useragent and regular expressions to determine the IE browser version, the following introduction to use IE browser different characteristics to determine IE browser

1 to judge IE browser and non IE browser

IE browser and non IE browser is the difference is that IE browser support activexobject, but not IE browser does not support ActiveXObject. When the IE11 browser has not yet appeared, we judge ie and non ie often write this

function Isie () {return
 window. ActiveXObject? True:false;
 }

But in the IE11, the judgment returned is false, and I tested the following code in IE11

Copy Code code as follows:

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 result, tell me why? For God's horse?

The official website of Microsoft said the difference of IE11 's activexobject. 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 following code to detect, it's OK.

Alert ("ActiveXObject" in Window)//returns false under Ie11

This is what I don't understand. The "ActiveXObject" in Window returns True, and why did the previous decision that IE's code returned false in IE11? Ask Daniel again for an explanation. Thank you
The following directly gives a compatible IE11 to judge IE and non IE browser methods.

function Isie () {return
 ("ActiveXObject" in window);
 }

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

2 Judge IE6 Browser

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

function IsIe6 () {
 //IE6 does not support Windows. XMLHttpRequest return
 Isie () &&!window. XMLHttpRequest;
 }

3 Judge IE7 Browser

Because the document mode is supported from IE8, it supports Document.documentmode. IE7 is not supported, but IE7 is supported for XMLHttpRequest objects. The method of judgment is as follows

function IsIe7 () {
 //only ie8+ supports Document.documentmode return
 Isie () && window. XMLHttpRequest &&!document.documentmode;
 }

4 Judge IE8 Browser

At the beginning of the IE9, Microsoft is slowly approaching the standard, and we call IE678 a non-standard browser, ie9+ and other such as Chrome,firefox browsers are called standard browsers. One of the differences between the two is. Let's test the following code. What's the return?

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

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

function IsIe8 () {
 //alert (!-[1,])//->ie678 return nan so! Nan is true standard browser returns-1 so!-1 is False return
 Isie () &&!-[1,]&&document.documentMode;
}

5 Judge IE9, IE10, IE11 browsers

From the IE8 browser is to support the JSON built-in objects, starting from IE10 to support the strict mode of JS, about JS in the strict mode please refer to this article http://www.jb51.net/article/75037.htm

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

6 Judging other browsers

/**** from the public class functions encapsulated in the project ***/
//detection function
var check = functions (r) {return
 r.test (navigator.userAgent.toLowerCase ());
 };
var statics = {
 /**
 * is the browser for the WebKit
 kernel
 /iswebkit:function () {return
 check (/webkit/);
 },< c12/>/**
 * is firefox browser
 /isfirefox:function () {return
 check (/firefox/);
 },
 /**
 * Is Google browser
 /ischrome:function () {return
 !statics.isopera () && check (/chrome/);
 },
 /**
 * is Opera browser/
 isopera:function () {return
 check (/opr/);
 },
 /**
 * Check for Safari browser/
 issafari:function () {
 //Google Chrome browser also includes Safari return
 ! Statics.ischrome () &&!statics.isopera () && check (/safari/);
 }
;

JS How to determine the version of IE browser including IE11

<! DOCTYPE html>  

The above code to achieve the function of judgment, the following describes its implementation principle, I hope to be able to bring help to friends in need.

Let's look at a piece of code:

Navigator.useragent

Screenshot of information under IE11:

The corresponding regular expression is then used to match. IE11 and previous versions of the browser still have a big difference, the previous version, this information contains MSIE,IE11, the new add Trident, followed by the browser version number, this should pay special attention to.

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.