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

May 16, 2016 pm 03:26 PM
ie browser js judgment

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

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

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

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

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

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

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

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>
Copy after login

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if the images on the webpage cannot be loaded? 6 solutions What should I do if the images on the webpage cannot be loaded? 6 solutions Mar 15, 2024 am 10:30 AM

Some netizens found that when they opened the browser web page, the pictures on the web page could not be loaded for a long time. What happened? I checked that the network is normal, so where is the problem? The editor below will introduce to you six solutions to the problem that web page images cannot be loaded. Web page images cannot be loaded: 1. Internet speed problem The web page cannot display images. It may be because the computer's Internet speed is relatively slow and there are more softwares opened on the computer. And the images we access are relatively large, which may be due to loading timeout. As a result, the picture cannot be displayed. You can turn off the software that consumes more network speed. You can go to the task manager to check. 2. Too many visitors. If the webpage cannot display pictures, it may be because the webpages we visited were visited at the same time.

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to set 360 speed browser compatibility mode How to set 360 speed browser compatibility mode Feb 24, 2024 am 10:49 AM

360 Speed ​​Browser is a popular browser application that allows users to access the Internet quickly and securely. In order to solve the problem of abnormal page display or inability to use functions normally, 360 Extreme Browser provides a compatibility mode function to allow users to browse the web better. So how to set the 360 ​​speed browser compatibility mode? Don’t worry, the editor will bring you a tutorial on how to set up the compatibility mode of 360 Extremely Fast Browser. I hope it can help you. How to set the compatibility mode of 360 Speed ​​Browser 1. Open the 360 ​​Speed ​​Browser downloaded from this site. After opening the software, click the three horizontal bars icon in the upper right corner. 2. Click [Settings] in the pop-up options. 3. Click in the opened 360 Speed ​​Browser settings window

How to set up trusted sites in win11_How to add trusted sites in IE in windows11 How to set up trusted sites in win11_How to add trusted sites in IE in windows11 May 08, 2024 pm 01:11 PM

1. First, we open the IE browser in our system, find the gear-shaped button in the upper right corner, and click it. 2. After clicking it, you will see a drop-down menu, find and click [Compatibility View Settings] 4. Then enter the URL that needs to be added in Add this website, and then click [Add] on the right.

What is a dual-core browser? What is a dual-core browser? Feb 20, 2024 am 08:22 AM

Dual-core browser is a browser software that integrates two different browser cores. The kernel is the core part of the browser, responsible for rendering web content and executing web scripts and other functions. Traditional browsers generally use only a single kernel, such as IE browser using Trident kernel, Chrome browser using WebKit/Blink kernel, Firefox browser using Gecko kernel, etc. The dual-core browser integrates two different cores into one browser, and users can freely switch between them as needed. The emergence of dual-core browsers

How to turn off ads in edge browser? Introduction to how to turn off ads in edge browser How to turn off ads in edge browser? Introduction to how to turn off ads in edge browser Mar 14, 2024 pm 03:49 PM

The edge browser is now a browser tool installed on Windows systems. Many users find that many advertising pop-ups appear when using it. Many people want to know how to set up these advertisements to close them. In response to this problem, Today's Software The tutorial is here to answer the questions for the majority of users. Next, let us take a look at the detailed steps. Introduction to how to turn off edge browser ads: 1. Enter the software, click the three-dot icon on the right side of the top of the page, and select "Settings" in the drop-down option menu. 2. In the new interface, click "Advanced" and then find "Use" under "Website Settings"

How to set compatibility mode in edge browser? Where to set edge browser compatibility settings? How to set compatibility mode in edge browser? Where to set edge browser compatibility settings? Apr 15, 2024 pm 02:46 PM

The edge browser is a browser software that many users like to use. It has a simple interface but complete functions. Some novices want to set the compatibility mode for the new version of the edge browser. How do they do it? This tutorial will give you detailed operation tutorials, I hope it will be useful to everyone. Edge browser compatibility mode setting 1. Click the start menu in the lower left corner of the computer and select Microsoft Edge browser. 2. After opening the Edge browser, click the “…” icon in the upper right corner. 3. Click the "Open with Internet Explorer" option in the pop-up options. 4. Open IE to browse

How to determine whether a date is the previous day in Go language? How to determine whether a date is the previous day in Go language? Mar 24, 2024 am 10:09 AM

Question: How to determine whether the date is the previous day in Go language? In daily development, we often encounter situations where we need to determine whether the date is the previous day. In the Go language, we can implement this function through time calculation. The following will be combined with specific code examples to demonstrate how to determine whether the date is the previous day in Go language. First, we need to import the time package in the Go language. The code is as follows: import(&quot;time&quot;) Then, we define a function IsYest

See all articles