Check the internet Internet Explorer version abroad! Now IE has reached 9, and IE checks the code.

Source: Internet
Author: User

In fact, learning the source code of Daniel is a good improvement, which 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:
Copy codeThe Code is as follows:
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 the ideas and codes of Daniel later)
Copy codeThe Code is as follows:
//----------------------------------------------------------
// A short snippet for detecting versions of IE in JavaScript
// Without resorting to user-agent sniffing
//----------------------------------------------------------
// 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 detect IE:
// If (ie ){}
// And to detect the version:
// Ie = 6 // IE6
// Ie> 7 // IE8, IE9...
// Ie <9 // Anything less than IE9
//----------------------------------------------------------
// 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:
Copy codeThe Code is as follows:
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 get 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:
Copy codeThe Code is as follows:
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.