Sometimes I will go and see some of the foreign Daniel's Code , And learn, reference, and lament that the big bull is the big bull, the gap is not a little bit, but also a little sigh of slowly pull up and the distance between the big bull. Actually learning Daniel Source code It is a very good progress and can give you a new perspective. Seeing this big bull's IE version check, you can only be amazed and amazed. There is too much content in the short code. So I decided to interpret the source code of Daniel here, so that it people who are preparing to move closer to Daniel and are still working hard can learn more from it.
Let's take a look at the world's shortest ie detection code:
VaR
Isie =! -[1,];
I am not familiar with it, but I am not able to detect ie9 if I have a bug. Why? This is because the foreign Daniel used IE to convert the array before ie9 came out. Ie9 has been repaired, so it is invalid in ie9, but as I was at that time, I still lament + lament, I am also addicted to how to use the shortest code to implement a function and method, and constantly modify and modify it .... this is the gap ).
I will not interpret and analyze the code of the world's shortest ie detection. After all, I have bugs and cannot be backward compatible. My focus is on the following perfect ie Detection, theoretically backward compatible, for example, ie10, OK, use it, no problem, another withdrawal gap.
Next let's take a look at the source code first (I will explain the difficulties in Daniel's thoughts and codes later.)
// Configure // a short snippet for detecting versions of IE in Javascript // without resorting to User-Agent sniffing // configure // if you're not in IE (or IE version is less than 5) then: // ie === undefined // if you're in IE (> = 5) Then you can determine which version: // ie === 7; // IE7 // Thus, to dete CT ie: // If (IE) {}// and to detect the version: // ie ===6 // IE6 // IE> 7 // IE8, ie9... // ie <9 // anything less than ie9 // restart // update: Now using live nodelist idea from @ jdalton var Ie = (function () {var UNDEF, V = 3, DIV = document. createelement ('div '), all = div. getelementsbytagname ('I'); While (div. innerhtml = '<! -- [If GT Ie '+ (++ v) +']> <I> </I> <! [Endif] --> ', all [0]); Return v> 4? V: UNDEF ;}());
A very brilliant code, but it can perfectly detect all versions of IE, and also a range-based detection, in the source code comment coach you how to use.
Principle:Dynamically create a DIV, insert an I tag in the IE condition comment, and check whether the I tag is added to determine whether it is an IE browser. In the while clause, the IE version is repeatedly compared.
Let's take a look at this Code:
VaR UNDEF,
V = 3,
DIV = Document. createelement ('div '),
All = div. getelementsbytagname ('I'); // This section is easy to understand. Declare variables and create a div to obtain the I
Div. innerhtml = '<! -- [If GT Ie '+ (++ v) +']> <I> </I> <! [Endif] --> ',
// This is the core. The IE condition annotation is used to complete the process. The IE condition annotation is backward compatible. Therefore, you can use ie10 after this detection, if the next version is ie10. For ie condition comments, you can find them on the Internet easily. [If IE 7] [If gt ie 7] has many modes. I will not describe this condition annotation here. If it is compatible with Web Front-end, I should be familiar with this.
Difficulties: While (
Div.Innerhtml = '<! -- [If GT Ie' + (++V) + ']> <I> </I> <! [Endif] -->',
All[0]
); What? While (expression 1, expression 2) What is this? Is it different from the while (expression) we learned? TIPS: if there are multiple expressions in while, the last expression is used as the exit judgment. No matter how many expressions there are, the previous expressions won't be used as the exit judgment, instead, it executes the code. For example, while (expression is 1, expression is 2, expression 3, expression 4) only uses true or false of expression 4 as the exit judgment. You can try it now. This is the code of Daniel. You can only be amazed and amazed!
It's just a few lines of code, so elegant. I hope you can learn the desired knowledge and broaden your horizons. The end.