Shortest IE judge var IE =! Analysis [1]

  • 2020-03-30 03:06:10
  • OfStack


 var ie = !+"v1";

Just 7bytes! See this article, 32 bytes, ehr... 9, ehr... 7!! To know if your browser is IE. But that record was broken by a Russian on January 8 this year. Now it's just 6 bytes! It takes advantage of the differences between Internet explorer and standard browsers in the toString method of dealing with arrays. For standard browsers, if the last character in the array is a comma, the JS engine will automatically remove it.

var ie = !-[1,];

This code before IE9 was known as the world's shortest IE determination code. The code is short but contains a lot of javascript basics. In this example, the code will first call the toString() method of the array, and execute [1,]. ToString () will get "1," in IE6,7,8. And then the expression becomes! - "1,". I'm going to try to convert "1, "to a numeric type to get NaN, and I'm going to take a negative on NaN and I'm going to get NaN again. Finally execute! NaN returns true. Here's a review of the javascript involved in the code by breaking it down:

1. Differences in array literal resolution in browsers

[1,] means that an array literal is defined using javascript. In IE6,7,8, the array has two elements, the value in the array is 1, undefined. In standard browsers, undefined after the first element is ignored, and the array contains only one element, 1.

2. Array toString() method

Calling the toString() method of the array object calls the toString() method for each element in the array, returns an empty string if the value of the element is NULL or undefined, and then spells the value of each item into a string separated by a comma ",".

Unary minus operator

When using the unary minus operator, if the operator is of numeric type, the operator is directly negative. Otherwise, the operator is first tried to convert to numeric type. The conversion process is equivalent to executing the Number function, and then the result is negative.

4. Logical nonoperations

Returns true if the operand is NaN, NULL, or undefined when performing a logical non-operation.

JavaScript can be written like this:

var ie = !-[1,];  

If from the point of view of non-ie, you can save a bit, because we do compatibility, the vast majority of the case is IE and non-ie start. Var notIE = - [1];

     alert(" This is not IE The browser! "); 
     alert(" This is a IE The browser! "); 

From the above knowledge, we can get the code var ie =! - [1]; That's the same thing as var ie =! (- Number ([1]. The toString ())); The median value is true in IE6\7\8.

Because ie6/7/8 doesn't ignore the [1,].ToString() bug, you get "1,"; And - Number ([1]. The toString ()) is - Number (" 1 "), the result is NaN. Then! (- Number ([1]. The toString ())) is! (NaN) gets true. The premise of everything is that ie6/7/8 has [1,].ToString()=>" 1," this bug, whereas other browsers [1,].ToString()=>" 1 ".

Recently, I found that some friends use this method to prompt users to upgrade their browser

!-[1,] && alert(' You are using  IE6-8  Version of the browser, nn Suggest using  Chrome, Firefox, IE9+  Browse! ');

Related articles: