) Browser Feature Detection (updated ie9 judgment)

Source: Internet
Author: User

[Update 2010.04.18]
Internet Explorer 9 is more rigorous,
Enhances the differentiation capability of opera,
And replaced Chrome's discriminative features with engine features [via],
In this way, you can see which browsers in China have been changed from the chrome kernel,
You can try it. :)

[Update 2010.04.17]
In my opinion, ie 9 is greatly improved:
Support for mutation events that I am concerned about,
There are also W3C event models ......
I will not go into details in this article,
I have the opportunity to discuss it in another article.
Because feature judgment is widely used in projects,
The most significant changes in IE 9 in this regard are:
Previously, the "shortest ie authentication method" has expired,
The array bug that has been accompanied by IE is fixed together:
The question of missing last element of a non-empty array literal (I .e. [null,]),
Therefore, the shortest Record of the current day is kept (6 bytes! -[1,]) is invalid. (CC compression is not supported)
Do not use the addeventlistener method to identify the cause,
Because ie 9 is supported. (The event has been greatly improved, which is good)
So Ie is getting closer and closer to the standard,
If there is any special processing for IE in the projectCode,
You may need to check again in ie9.

As I am a loyal fan of XP,
XP cannot install ie 9,
Therefore, some tests were completed with the assistance of Haitao Jia,
Thank you.
That's why,
I did not perform tests with wide coverage.
However, the only "surviving" ie series bug found is still related to the array bug just mentioned,
Microsoft fixed the bug in the literal but forgot that when the string is split into an array, it still sounds sound.
Although it seems a little troublesome,
But in any case, I have found only a few pieces of code that can be used to determine the full range of IE code:

! ','. Split (/,/). Length

Please drag the specific code to the end of the article.

[Update 2010.03.18]
IE 8 also supports the window. datatransfer drag method,
Therefore, the condition for determining the gecko kernel has been changed,
You can exclude IE8 from the original judgment conditions,
Alternatively, select the special gecko attribute provided starting with ff3.6, such as window. javasinnerscreenx,
(The corresponding retrieve ordinate Property Window. mozinnerscreenx)
There is also a method to check whether a node meets a selector rule node. extends matchesselector (very practical)

[Update 2010.01.21]
Google's closure compile will "compress" the IE judgment code:

! + "\ U000b1"

Either compress and replace it! + "\ V1 ",
You can also use other safer methods to Determine IE.

[Published in 2010.01.03]
Before writing this article,
Chen Cheng told me that Master Nicholas C. zakas just wrote an article named
Feature Detection is not browser Detection
OfArticle.
The article "profoundly" criticizes the feature detection method used by mootools,
However, the convincing reason does not seem to be reflected in the text,
It is said that mootools was forced to release an upgrade due to changes in Firefox 3.6,
Then we say:

"It will become increasingly difficult and dangerous to distinguish browsers (features) from each other as they become closer to each other.
(As browsers grow closer together, looking at "features" to separate them will become more difficult and risky .)"

Of course,
Not only JavaScript,
The server must also rely on User-Agent (UA) to count the client ),
For UA spoofs,
Nicolas's opinion is:

"You must always respect the UA that the browser tells you.
(You shoshould always honor exactly what the browser is reporting as a User-Agent .)"

Therefore, from the back-end perspective,
I personally agree with this point of view.

But on the other hand,
Although the browser kernel is relatively fixed,
However, there are also a lot of browsers integrated with multiple kernels,
And their share in China is not generally high,
(However, UA management is not generally bad)
I think this is something that Master Ni did not expect.
(It is worth mentioning that the web-kit mode of toilet 3 provides UA feature characters this time)

Therefore, I don't think it's a good choice for feature detection,
However, the UA sniffing method cannot be regarded as an alternative,
The ability to accurately determine the browser and then conduct the correct hack to ensure the complete experience is king.

Er ......
Write so much,
It is entirely for the new words of Master Ni,
The text I expected starts from here --

Due to the huge changes in Firefox 3.6,
(Of course, not only the Javascript layer,
For example, the old Chrome registration file contents. RDF is also abolished)
We should treat version 3.6 as a milestone version of Firefox.
For this article,
That is to say, the features used to distinguish Firefox have disappeared:
For example, the most well-known window. getboxobjectfor (),
For example, the trick of/A/[-1] = 'A.
Therefore, we must find a new feature to fill in this vulnerability,
(It is extremely difficult to find a feature that persists from Firefox 1.0)
After reading the documentation, you can find
Window. datatransfer (),(Also supported by Internet Explorer 8)
It is provided by Firefox to obtain the Properties window. xilinnerscreenx/y relative to the window horizontal/vertical coordinates in the visible area,
The latest Firefox 3.7a1pre nightly is also supported.

Therefore, after sorting and summarizing,
I personally agree that the feature detection method is as follows:

(Function (Win, DOC ){
VaR isie =! ','. Split (/,/). length,
Isie6 = isie &&! Win. XMLHttpRequest,
Isie8 = !! Win. xdomainrequest,
Isie9 = isie &&!! + '\ V1', // alt :!! -[1,]
Isie7 = isie &&! Isie6 &&! Isie8 &&! Isie9,
Isff = !! Doc. getboxobjectfor | 'your innerscreenx' in win, // Gecko
Isop = !! Win. Opera &&!! Win. Opera. tostring (). indexof ('Opera '),
Isop9 =/^ function \ (/. Test ([]. Sort ),
Iswk = !! Win. devicepixelratio, // web-Kit
ISSF =/A/. _ PROTO _ = '/', // Safari
Iscr =/S/. Test (/A/. tostring); // chrome
}) (Window, document );

It is worth mentioning that the web-kit kernel,
Window. devicepixelratio () can be used to distinguish all web-kit based browsers,
This includes the speed mode of Maxthon 3 and other similar browsers that have not yet been released.
Chrome has one more window. messageevent than safari,
However, be sure to exclude Firefox.

Finally,
I also sorted out the feature profiling method for Firefox's major milestones,
It is not recommended to be included in general judgment methods,
However, if you are plagued by the differences between these versions,
They should be useful:

(Function (WIN ){
VaR aboveff36 = 'your innerscreenx' in win, // alt :!! Document. Body. Specify matchesselector
Aboveff35 = !! Object. getprototypeof,
Aboveff2 = !! Win. globalstorage;
Aboveff3 = upperff2 &&!! Win. messageevent, // alt: 'reduce' in array
Aboveff15 = 'some' in array;
}) (Window );

Among them, 3.0 and 3.6 are two major milestones I personally think are the greatest changes,
You can use it at any time.
In addition,
Major version changes in Firefox almost always follow the upgrade of JavaScript,
Therefore, you can use 'reduce' in array to judge version 3.0,
The premise is that you have not extended the array.

Ref:
Ultimate JavaScript browser judgment skills
Determine the browser type and version using JavaScript
(The above two articles are all very good,
Only Firefox and Safari need to be updated)
Detecting browsers JavaScript hacks (recommended)

Very messy and complex,
This is not the case ......

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.