Home  >  Article  >  Web Front-end  >  Summary of several judgment methods for js ie non-ie browsers_javascript skills

Summary of several judgment methods for js ie non-ie browsers_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:26:401151browse

Recently, in the article of Situ Zhengmei js The World's Shortest IE Browser Determination Code , I saw a method of judging ie or non-ie that only has 6 bytes. The code is as follows:


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]



In fact, there are many ways to judge, most of which are based on the characteristics of the browser.

For example, the library prototype method is: !!(window.attachEvent && navigator.userAgent.indexOf('Opera') === -1) . It is judged based on the fact that IE supports window.attachEvent to add listening events, and non-IE uses window.addEventListener to add listening events. navigator.userAgent.indexOf('Opera') === -1 is because the opara browser can disguise itself as ie. If !!(window.attachEvent) is true, it is ie; conversely, if !window.addEventListener is true, it is also It can be judged as ie.

Ext uses !"1"[0], which is judged by taking advantage of the fact that IE cannot use array subscripts to access strings. There seems to be a problem under ie8.

Before ! [1,] was discovered, the shortest expression to judge IE was ! "v1". It takes advantage of the fact that IE does not support vertical tab characters.

In the past, another commonly used method was document.all, because the Opera browser can disguise itself as IE. You can write like this:!!(document.all && navigator.userAgent.indexOf('Opera') === -1).

There are many more, so remember these few first for easy reference at work.

1. [1,]

2.! "v1"

3.!!(window.attachEvent && navigator.userAgent.indexOf('Opera') = == -1)

4.!!(!window.addEventListener&& navigator.userAgent.indexOf('Opera') === -1)
<script> if(!+[1,])alert("这是ie浏览器");    else alert("这不是ie浏览器"); </script>5.!!(document.all && navigator.userAgent.indexOf('Opera') === -1)
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