Js recommended for quick judgment of IE browser (compatible with IE10 and IE11) _ javascript skills

Source: Internet
Author: User
This article mainly introduces the js quick judgment IE browser (compatible with IE10 and IE11) that is recommended for reading, because ie11 has canceled the judgment of many lower versions and many js files need to be modified, here is a brief introduction to the need of friends can refer to determine whether IE browser is using window. navigator. userAgent, tracking this information, found in the development environment, identified as IE10, but access server is identified as IE11, but IE11 userAgent does not have the MSIE Mark, this is the reason.

Change the method of judging IE browser to the following.

function isIE() { //ie? if (!!window.ActiveXObject || "ActiveXObject" in window) return true; else return false; }

The following are some examples of practical analysis.

In many cases, we generally use navigator. userAgent and regular expression to determine the version of the IE browser. The following describes how to use different features of the IE browser to determine the version of the IE browser.

1. Judge IE and non-IE browsers

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

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

However, in IE11, the returned value is false. I tested the following code on IE11:

The Code is as follows:

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

The result is

Why? Obviously, ActiveXObject exists, and the result of typeof is indeed undefined. Who knows the result and tells me why? Why?

On the official website of Microsoft, the ActiveXObject of IE11 is different. Http://msdn.microsoft.com/en-us/library/ie/dn42394820.28v?vs.85=29.aspx. However, the reason for typeof is not explained. If we use the following code to detect the problem

Alert ("ActiveXObject" in window) // false is returned in ie11.

I don't understand why the returned value of "ActiveXObject" in window is true. Why did I judge that the returned value of the IE browser code in IE11 is false? Ask the experts for an explanation again. Thank you.
The following describes how to determine whether the Internet Explorer and Internet Explorer are compatible with Internet Explorer 11.

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

Note that the precondition is that we should not overwrite ActiveXObject in the program code. No program should do this. Haha.

2 Judge IE6 Browser

IE supports XMLHttpRequest objects since IE7, but IE6 does not. Based on this feature and the previous function isIe () of IE, we will know how to judge IE6. The judgment method is as follows:

Function isIe6 () {// ie6 does not support return isIe () of window. XMLHttpRequest ()&&! Window. XMLHttpRequest ;}

3 judge IE7 Browser

Because the document format is supported from ie8, document.doc umentMode is supported. IE7 is not supported, but IE7 supports XMLHttpRequest objects. The judgment method is as follows:

Function isIe7 () {// only ie8workflow supports document.doc umentMode return isIe () & window. XMLHttpRequest &&! Document.doc umentMode ;}

4 judge IE 8 browser

Since IE9, Microsoft has gradually approached the standard. We call IE678 a non-standard browser. IE9 + and other browsers such as chrome and firefox are called standard browsers. One of the differences is. You can test the following code. What is returned

Alert (-[1,]); // print NaN in IE678, but-1 in standard browser

Then we can determine that the Internet Explorer is an Internet Explorer 8 Based on the difference above. The method is as follows:

Function isIe8 () {// alert (! -[1,]) //-> IE678 returns NaN! If NaN is true, the standard browser returns-1, so! -1 is false return isIe ()&&! -Example 1, example &document.doc umentMode ;}

5 Judge IE9, IE10, and IE11 browsers

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

IE9 + alert (! -[1,]) the returned value is false. IE9 + supports addEventListener, but the unique events in IE cannot be bound to attachEvent in IE11. Based on these differences, we can differentiate IE9, IE10, and IE11 browsers.

6. determine other browsers

/***** From the public class functions encapsulated in previous projects *** // The detection function var check = function (r) {return r. test (navigator. userAgent. toLowerCase () ;}; var statics ={/ *** whether it is a webkit kernel browser */isWebkit: function () {return check (/webkit /);}, /*** is firefox browser */isFirefox: function () {return check (/firefox/);},/*** is it Google browser */isChrome: function () {return! Statics. isOpera () & check (/chrome/) ;},/*** whether it is an operabrowser */isOpera: function () {return check (/opr /);}, /*** check if it is a Safari browser */isSafari: function () {// safari return is also included in google chrome! Statics. isChrome ()&&! Statics. isOpera () & check (/safari /);}};

How does js determine the version of IE browser, including IE11?

 Foot home

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.