Several judgment methods of JS ie non-IE browser summary _javascript skills

Source: Internet
Author: User
Recently in Masaki JS World's shortest IE browser to judge the code of the article, see only 6byte to judge ie and non ie method. The code is as follows:
<script> if (!+[1,]) alert ("This is IE browser"); Else alert ("This is not IE browser"); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]



In fact, there are many ways to judge, mostly according to the characteristics of the browser.

For example, the library prototype method is:!! (Window.attachevent && navigator.userAgent.indexOf (' Opera ') = = 1). is based on IE support window.attachevent to add listening events, not IE use Window.addeventlistener to add listening events to judge. Navigator.userAgent.indexOf (' Opera ') = = 1 is because the Opara browser can be disguised as ie. if!! (window.attachevent) is true, ie; conversely, if!window.addeventlistener is true, it can also be judged as ie.

EXT is using! " 1 "[0], he uses IE cannot use the array subscript to access the character of the string to judge. There seems to be a problem under the IE8.

In!+[1,] before being discovered, the expression that determines IE shortest is!+ "\v1". It utilizes the feature that IE does not support vertical tabs.

Previously there was a common method of document.all, because opera browser can be disguised as ie. Can write like this:!! (document.all && navigator.userAgent.indexOf (' Opera ') = = 1).

There are many, first remember these several, easy to consult when working.

1.+[1,]

2.!+ "\v1"

3.!! (Window.attachevent && navigator.userAgent.indexOf (' Opera ') = = = 1)

4.!! (!window.addeventlistener&& navigator.userAgent.indexOf (' Opera ') = = 1)

5.!! (document.all && navigator.userAgent.indexOf (' Opera ') = = = 1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.