In the past, the shortest IE determination was developed by virtue of the feature that IE does not support vertical tabs.
Copy codeThe Code is as follows:
Var ie =! + "\ V1 ";
Only 7 bytes! See this article, "32 bytes, ehr... 9, ehr... 7 !!! To know if your browser is IE, describes how foreigners reduce IE's judgment from 32 bytes to 7 bytes step by step! But this record was broken by a Russian in January 8 this year. Now it only takes 6 bytes! It makes use of the difference between IE and the standard browser in handling the toString method of arrays. For the standard browser, if the last character in the array is a comma, The JS engine will automatically remove it.
Copy codeThe Code is as follows:
Var ie =! -[1,];
This code was called the world's shortest IE judgment code before IE9. The code is short but contains a lot of basic javascript knowledge. In this example, the toString () method of the array will be called before code execution, and the [1,]. toString () method will get "1," in IE6, 7, and 8 ,". Then the expression is changed! -"1 ,". Then try to convert "1," to the numerical type to get NaN, and then take negative NaN to get the value still NaN. Last executed! NaN returns true. Next, we will use this statement to review the javascript knowledge involved in the Code:
1. Differences in array literal parsing of the browser
[1,] indicates that an array is defined literally using an array of javascript. In IE6, 7, and 8, the array has two elements: 1 and undefined. The undefined after the first element is ignored in the standard browser, and the array contains only one element 1.
2. array toString () method
When you call the toString () method of an array object, the toString () method is called for each element in the array. If the element value is NULL or undefined, an empty string is returned, then, the values of each item are combined into a string separated by commas.
3. unary minus sign Operator
When the unary minus sign operator is used, if the operation Number is of the numerical type, the Operation Number is directly negative. Otherwise, the Operation Number is first converted to the numerical type. The conversion process is equivalent to executing the Number function, then, the negative result is obtained.
4. Logical non-operation
True is returned if the operand is NaN, NULL, or undefined when the logical non-operation is performed.
JavaScript can be written like this:
Copy codeThe Code is as follows:
Var ie =! -[1,];
Alert (ie );
If we judge from the Perspective of Non-IE, we can save a bit, because when we are compatible, most of the cases are IE and non-IE. Var notIE =-[1,];
Copy codeThe Code is as follows:
If (-[1,]) {
Alert ("this is not an Internet Explorer! ");
} Else {
Alert ("this is IE browser! ");
}
Through the above knowledge, we can get the code var ie =! -[1,]; in fact, it is equivalent to var ie =! (-Number ([1,]. toString (); the value is true in IE6 \ 7 \ 8.
Because neither IE6 nor 7/8 ignores [1,]. toString (), that is, the result is "1,"; and-Number ([1,]. toString () is-Number ("1,"). The result is NaN. then! (-Number ([1,]. toString () is! (NaN) returns true. The premise is that IE6/7/8 has the bug [1,]. ToString () => "1,", while other browsers (it should be most of it ~~) It is [1,]. ToString () => "1 ".