Home > Web Front-end > JS Tutorial > body text

JavaScript determines IE version model_Basic knowledge

WBOY
Release: 2016-05-16 15:48:55
Original
1029 people have browsed it

The following will explain it to you through the code. Please see below for details:

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

Copy code The code is as follows:

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

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

Copy code The code is as follows:

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

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.

Copy code The code is as follows:

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

The following is a direct method to determine whether IE and non-IE browsers are compatible with IE11.

Copy code The code is as follows:

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

Judge 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:

Copy code The code is as follows:

function isIe6() {
// ie6 does not support window.XMLHttpRequest
Return isIe() && !window.XMLHttpRequest;
}

Judge 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:

Copy code The code is as follows:

function isIe7() {
//Only IE8 supports document.documentMode
Return isIe() && window.XMLHttpRequest && !document.documentMode;
}

Judge IE8 browser

Starting from IE9, Microsoft is slowly moving 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 alert(-[1,]);//In IE678, NaN is printed, but in standard browsers, -1

Then we can judge it is an IE8 browser based on the above differences. Here’s how:

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

Judge IE9, IE10, IE11 browsers

The browser supports JSON built-in objects from IE8, and supports the strict mode of js starting from IE10. Under IE9, alert(!-[1,]) 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.

Judge 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/)
   }
 };
Copy after login

The above is the entire introduction of this article, I hope it can help everyone.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template