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
But in IE11, the above judgment returns false. I tested the following code in IE11:
The official website on Microsoft explains the differences in IE11’s ActiveXObject.
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:
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:
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; }
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/) } };
The above is the entire introduction of this article, I hope it can help everyone.