No matter what version of IE browser it is, it is always somewhat incompatible with Web standards. For code workers, it is naturally miserable. In order to consider the compatibility issue of IE, whether writing CSS or JS, IE is often treated specially, which requires some judgment. This article does not discuss how to distinguish the styles of IE, only JS determines the IE browser.
The world's shortest Javascript method to determine IE browser comes from Russia! It has been tested on various versions of IE and other currently popular browsers. Although Microsoft has been aware of IE-based bugs, it has never corrected them.
<script type='text/javascript'> var ie = !-[1,]; alert(ie); </script> |
The above code execution result: returns true under IE, and returns false under other standard browsers. !-[1,], only 6 bytes!
However, if the judgment is reversed, if the standard browser returns true and IE returns false, then it can be shortened by one byte.
<script type='text/javascript'> notIe = -[1,]; if(-[1,]){ // Standard browser code }else{ // Code for IE Only } </script> |
After reading this, are you curious about how these work? Please continue reading below.
This bug occurs because IE adds an empty array element to the total number of array elements.
[1,]. Length standard browsers will return 1 (based on standard ECMAscript, the comma "," at the end of the array will be ignored, this is to facilitate display in a column and automatic generation, etc.), but IE will return 2. When you print this array IE will return "1, ", which is two elements, while other standard browsers will return "1".
This is easy to verify by running the following code in IE and FF:
<script type='text/javascript'> alert([,]==','); //This is 8 characters to determine IE </script> |
[1,] In fact, the operation of the browser is toString() to convert the string into a string, and -[1,] is to force the string to a number. IE will return NaN, but unfortunately NaN is not a number, because "1," after [1,] is converted into a string, contains a comma. Other standard browsers will return -1, which is a non-zero number.
You know, converting NaN to Boolean will return false, so -[1,] will return false under IE. Any non-0 number converted to Boolean type (such as -1) will return true under standard browsers. So we get a judgment result, !-[1,] returns true under IE, but returns false under other standard browsers. This also achieves the purpose of distinguishing and judging IE browsers.
Of course, as mentioned before, Microsoft has actually known about this bug for a long time, but has never fixed it, so in the future > IE8 IE browser is not sure whether it will still work, but basically after so many generations IE has not been repaired, and it is unlikely that IE will be repaired in the future.
The following are some other codes for distinguishing and judging IE browsers, you can also refer to the following:
<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> |