The shortest IE judgment in the past was made by the feature that IE does not support vertical tabs.
The code copy is as follows:
var ie = !+"/v1";
Just 7bytes are needed! See this article, "32 bytes, ehr ... 9, ehr ... 7!!! to know if your browser is IE", tells the story of how foreigners reduced the judgment of IE from 32 bytes to 7 bytes! But this record was broken by a Russian on January 8 this year. Now it only takes 6 bytes! It uses the difference between IE and standard browsers in processing arrays. For standard browsers, if the last character in the array is a comma, the JS engine will automatically remove it.
The code copy is as follows:
var ie = !-[1,];
This code was called the world's shortest IE judgment code before IE9. Although the code is short, it does contain a lot of basic javascript knowledge. In this example, when the code is executed, the toString() method of the array will be called first. Execute [1,].toString() in IE6, 7, 8 will get "1,". Then the expression becomes!-"1,". Then try to convert "1," into a numerical type to obtain NaN, and then take a negative NaN to obtain the value still NaN. Finally execute!NaN returns true. The following is to review the javascript knowledge involved in the code by decomposing this statement:
1. The browser's array literal parsing difference
[1,] means that an array is defined using javascript's array literal. In IE6, 7, 8, there are two elements of the array, and the values in the array are 1, undefined. 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
When calling the toString() method of the array object, the toString() method will be called on each element in the array. If the value of the element is NULL or undefined, an empty string will be returned, and the value of each item will be spelled into a string separated by commas "," .
3. One-ary minus operator
When using the unary minus operator, if the operation number is a numeric type, it will directly negate the operation number. Otherwise, you will first try to convert the operation number to a numeric type. The conversion process is equivalent to executing the Number function, and then negative the result.
4. Logical non-operation
Returns true if the operand is NaN, NULL, or undefined when performing logical non-operations.
JavaScript can be written like this:
The code copy is as follows:
var ie = !-[1,];
alert(ie);
If we judge from a non-IE perspective, we can save one bit, because when we are compatible, most of the time, IE and non-IE start construction. var notIE = -[1,];
The code copy is as follows:
if(-[1,]){
alert("This is not IE browser!");
}else{
alert("This is IE browser!");
}
From the above knowledge, we can obtain that the code var ie = !-[1,]; is actually equivalent to var ie = !(-Number([1,].toString())); The value in IE6/7/8 is true.
Because IE6/7/8 will not ignore the bug [1,].ToString(), that is, what you get is "1,"; and -Number([1,].toString()) is -Number("1,") The result is NaN; then !(-Number([1,].toString())) is !(NaN) is true. The premise of everything is that IE6/7/8 has the bug [1,].ToString()=>"1," while other browsers (that should be most of them~~) are [1,].ToString()=>"1".
Recently I found that a friend used this to prompt users to upgrade their browser
<script>!-[1,] && alert('You are using IE6-8 version of the browser. It is recommended to use Chrome, Firefox, IE9+ to browse with /n/n!');</script>