The shortest IE judgment var ie=!-[1,] analysis _javascript skill

Source: Internet
Author: User
Tags arithmetic

The previous shortest IE decision by the use of IE does not support the characteristics of vertical tabs out.

Copy Code code as follows:

var ie =!+ "\v1";

Just need 7bytes! See this article, "bytes, EHR ..." 9, EHR ... 7!!! To know if your browser is IE, tell the story of how foreigners are taking ie's judgment from bytes to 7 bytes!. But the record was broken by a Russian this January 8, now just 6 bytes! It makes use of the difference between IE and standard browsers in handling the array's ToString method. For a standard browser, if the last character in the array identifier a comma, the JS engine automatically strips it.

Copy Code code as follows:

var ie =!-[1,];

This code was previously known as the world's shortest ie decision code before IE9. The code is short but contains a lot of JavaScript basics in it. In this example, the code executes with the array's toString () method called First, and execution [1,].tostring () will get "1" in ie6,7,8. The expression then becomes!-"1,". Then try to convert "1," to a numeric type to get Nan, and then a negative value for Nan is still nan. Final Execution! Nan returns True. The following is a review of the JavaScript knowledge involved in the code by decomposing this statement:

1. The browser's array literal resolution difference

[1,] means that an array is defined using the literal number of JavaScript. In ie6,7,8, the array has two elements, and the values in the array are 1,undefined respectively. In a standard browser, the undefined after the first element is ignored, and the array contains only one element 1.

2. The ToString () method of the array

Calling the ToString () method of an Array object invokes the ToString () method for each element in the group, if the value of the element is null or undefined, the empty string is returned, and the resulting value of each item is spelled into a string separated by a comma ",".

3. Unary minus operator

When using the unary minus operator, if the arithmetic is a numeric type, the value is directly negative, otherwise it will try to convert the arithmetic into the numeric type, the conversion process is equivalent to executing the number function, and then the result is negative.

4. Logical non-operation

Returns true if the operand is Nan, null, or undefined when performing a logical negation.

JavaScript can write this:

Copy Code code as follows:

var ie =!-[1,];
alert (IE);

If from the angle of non ie, can save a bit, because we do compatibility, most of the situation is IE and non ie to start. var notie =-[1,];

Copy Code code as follows:

if (-[1,]) {
Alert ("This is not IE browser!") ");
}else{
Alert ("This is IE browser!") ");
}

Through the above knowledge can draw code var ie =!-[1,]; In fact, equivalent to the var ie =! (-number ([1,].tostring ())]; The value in Ie6\7\8 is true.

Because IE6/7/8 will not ignore [1,]. ToString () This bug gets "1," and-number ([1,].tostring ()) is-number ("1,") and the result is Nan; (-number ([1,].tostring ())] is! (NaN) is true. All the premise is that IE6/7/8 have [1]. ToString () => "1," this bug, and other browsers (should be most of it ~ ~) is [1,]. ToString () => "1".

Recently found a friend to use to prompt the user to upgrade the browser

<script>
!-[1,] && alert (' You are using the ie6-8 version of the browser, \ n \ nyou suggest Chrome, Firefox, ie9+ browsing! ');
</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.