The simplest and most short JavaScript Method for Determining ie browsers

Source: Internet
Author: User

Regardless of the version, ie is always incompatible with web standards. ForCodeFor workers, it is naturally miserable. In order to consider the compatibility of IE, whether it is CSS or JS, it is often necessary to treat ie specially, which makes some judgment. This article does not discuss how to differentiate ie styles, but only determine the IE browser by Js.

This is currently the world's shortest JavaScript Method for Determining ie browsers from Russia! It has been tested in various versions of IE and other popular browsers. Although Microsoft has realized the bug Based on IE, it has never been corrected.

 

 
<SCRIPT type = 'text/JavaScript '> var Ie =! -[1,]; alert (IE); </SCRIPT>

 

Run the preceding code: True is returned for IE, and false is returned for other standard browsers .! -[1,], only 6 bytes!

However, if the standard browser returns true and IE returns false, a byte can be shortened.

 

 
<SCRIPT type = 'text/JavaScript '> notie =-[1,]; If (-[1,]) {// standard browser code} else {// ie only code} </SCRIPT>

 

After reading this, are you curious about how this works? Continue with the following.

This bug occurs because IE adds an empty array element to the total number of array elements.

[1,]. the length standard browser will return 1 (based on the standard ecmascript, the last comma in the array "," will be ignored, this is to facilitate display in a column and Automatic Generation, etc ), but ie returns 2. When you print this array, ie will return "1,", that is, two elements, while other standard browsers will return "1 ″.

This is easy to verify, such as running the following code in IE and FF:

 

 
<SCRIPT type = 'text/JavaScript '> alert ([,] =', '); // This is an 8-character ie determination. </SCRIPT>

 

[1,] In fact, the browser operation is to convert tostring () to a string,-[1,] is to forcibly convert the string to a number. IE will return Nan, but unfortunately Nan is not a number, because [1,] is converted into a string with a comma in "1. Other standard browsers return-1, which is a non-zero number.

As you know, if Nan is converted to boolean, false is returned, so-[1,] false is returned in IE. If any number other than 0 is converted to a Boolean value (for example,-1), true is returned in the standard browser. So we get a judgment result ,! -[1,] returns true in IE, and false in other standard browsers. This achieves the purpose of distinguishing ie browsers.

Of course, as mentioned above, Microsoft has known this bug for a long time, but it has never been fixed. Therefore, in the future, the IE browser of IE8 will not be sure whether it will continue to work, however, basically all the generations of IE have not been repaired, and IE will not be fixed in the future.

The following are some other code for distinguishing ie browsers. For details, refer:

 

<SCRIPT type = 'text/JavaScript '> // option from Dean Edwards: var Ie =/* @ cc_on! @ */False; // use the commented line: var ie // @ cc_on = 1; // variation (shorter variable ): vaR Ie = '\ V' = 'V'; // option to Gareth Hayes (former record-holder): var Ie =! + "\ V1"; </SCRIPT>

 

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.