Home >Web Front-end >JS Tutorial >Recommended reading of js to quickly determine IE browser (compatible with IE10 and IE11)_javascript skills

Recommended reading of js to quickly determine IE browser (compatible with IE10 and IE11)_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:26:121789browse

Determine whether the IE browser uses window.navigator.userAgent. Track this information and find that in the development environment, it is recognized as IE10, but when accessing the server, it is recognized as IE11, but there is no MSIE mark in the userAgent of IE11. The reason is This is it.


Just change the method of judging IE browser to the following.

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

Here are some sharings, you can take a look at them, very practical analysis and explanation

In many cases, we generally use navigator.userAgent and regular expressions to determine the IE browser version. Here is an introduction to using different features in IE browser to determine IE browser

1 Determine 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. Before the IE11 browser appeared, this was how we often judged IE and non-IE

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

But in IE11, the above judgment returns false. I tested the following code in IE11 myself

Copy code The code is as follows:

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

The result is

Why is this? Obviously ActiveXObject exists, but the result of typeof is indeed undefined. Anyone who knows the result can tell me why? For Shenma?

The official website on Microsoft explains the differences in IE11’s ActiveXObject. http://msdn.microsoft.com/en-us/library/ie/dn423948%28v=vs.85%29.aspx. But the reason for typeof is not explained. It is ok if we use the following code to detect

alert("ActiveXObject" in window)//Returns false under ie11

This is what I don’t understand again. "ActiveXObject" in window returns true. Why did the code used to judge the IE browser return false in IE11? Again, I beg the experts to give me an explanation. Thank you
The following is a direct method to determine whether IE and non-IE browsers are compatible with IE11.

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

Note that the prerequisite is not to overwrite ActiveXObject in our program code. No program should do this. hehe.

2 Determine IE6 browser

Starting from IE7, IE supports the XMLHttpRequest object, but IE6 does not support it. Based on this feature and the previous function isIe() to judge IE, we know how to judge IE6. The judgment method is as follows

function isIe6() {
 // ie6是不支持window.XMLHttpRequest的
 return isIe() && !window.XMLHttpRequest;
 }

3 Determine IE7 browser

Because document mode is supported starting from IE8, it supports document.documentMode. IE7 does not support it, but IE7 supports the XMLHttpRequest object. The judgment method is as follows

function isIe7() {
 //只有IE8+才支持document.documentMode
 return isIe() && window.XMLHttpRequest && !document.documentMode;
 }

4 Determine IE8 browser

Starting from IE9, Microsoft has slowly moved closer to the standard. We call IE678 a non-standard browser, and IE9+ and other browsers such as chrome and firefox are called standard browsers. One of the differences between the two is. Please test the following code. What is returned

alert(-[1,]);//What is printed in IE678 is NaN, but what is printed in standard browsers is -1

Then we can judge it is an IE8 browser based on the above differences. The method is as follows

function isIe8(){
 // alert(!-[1,])//->IE678返回NaN 所以!NaN为true 标准浏览器返回-1 所以!-1为false
 return isIe() &&!-[1,]&&document.documentMode;
}

5 Determine IE9, IE10, IE11 browsers

The browser supports JSON built-in objects from IE8, and supports the strict mode of js starting from IE10. Please refer to this article for the strict mode in JShttp://www.jb51.net/article/75037 .htm

Alert(!-[1,]) under IE9+ returns false. IE9+ supports addEventListener, but the IE11 browser does not support the original event binding attachEvent unique to IE. Based on these differences, we can distinguish IE9, IE10, and IE11 browsers.

6 Determine other browsers

/****来自曾经项目中封装的公共类函数***/
//检测函数
var check = function(r) {
 return r.test(navigator.userAgent.toLowerCase());
 };
var statics = {
 /**
 * 是否为webkit内核的浏览器
 */
 isWebkit : function() {
 return check(/webkit/);
 },
 /**
 * 是否为火狐浏览器
 */
 isFirefox : function() {
 return check(/firefox/);
 },
 /**
 * 是否为谷歌浏览器
 */
 isChrome : function() {
 return !statics.isOpera() && check(/chrome/);
 },
 /**
 * 是否为Opera浏览器
 */
 isOpera : function() {
 return check(/opr/);
 },
 /**
 * 检测是否为Safari浏览器
 */
 isSafari : function() {
 // google chrome浏览器中也包含了safari
 return !statics.isChrome() && !statics.isOpera() && check(/safari/);
 }
};

How does js determine the version of IE browser including IE11

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.jb51.net/" />
<title>脚本之家</title>
<script type="text/javascript">
var userAgent = navigator.userAgent, 
rMsie = /(msie\s|trident.*rv:)([\w.]+)/, 
rFirefox = /(firefox)\/([\w.]+)/, 
rOpera = /(opera).+version\/([\w.]+)/, 
rChrome = /(chrome)\/([\w.]+)/, 
rSafari = /version\/([\w.]+).*(safari)/; 
var browser; 
var version; 
var ua = userAgent.toLowerCase(); 
function uaMatch(ua){ 
 var match = rMsie.exec(ua); 
 if(match != null){ 
 return { browser : "IE", version : match[2] || "0" }; 
 } 
 var match = rFirefox.exec(ua); 
 if (match != null) { 
 return { browser : match[1] || "", version : match[2] || "0" }; 
 } 
 var match = rOpera.exec(ua); 
 if (match != null) { 
 return { browser : match[1] || "", version : match[2] || "0" }; 
 } 
 var match = rChrome.exec(ua); 
 if (match != null) { 
 return { browser : match[1] || "", version : match[2] || "0" }; 
 } 
 var match = rSafari.exec(ua); 
 if (match != null) { 
 return { browser : match[2] || "", version : match[1] || "0" }; 
 } 
 if (match != null) { 
 return { browser : "", version : "0" }; 
 } 
} 
var browserMatch = uaMatch(userAgent.toLowerCase()); 
if (browserMatch.browser){ 
 browser = browserMatch.browser; 
 version = browserMatch.version; 
} 
document.write(browser+version);  
</script>
</script>
</head>
<body>
 
</body>
</html>

The above code implements the judgment function. Here is an introduction to its implementation principle. I hope it can help friends in need.

Let’s look at a piece of code first:

navigator.userAgent

Screenshot of information under IE11:

Then use the corresponding regular expression to match. There are still big differences between IE11 and previous versions of the browser. In the previous version, this information contained msie, but it is no longer in IE11. Trident is newly added, followed by the version number of the browser. Pay special attention to this.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn