Comprehensive browser useragent judgment code

Source: Internet
Author: User
Tags user agent switcher

What is the user agent? Anyone who understands how to create a web page should understand it. In short, the user agent is used to identify information such as the browser name, version, engine, and operating system. Therefore, the judgment of the User Agent becomes the key to identifying the browser. In addition, mobile Internet development is gaining momentum, so it is important to judge the desktop devices or mobile devices through the user agent. Of course, the user agent can also be used to improve the compatibility. For example, it is determined that the user uses the IE 6 browser to use differentCode. I think many web development projects have become the first issue to be considered.

Is there any good code for judging the user agent? There should be a lot on the Internet. It is relatively simple to directly judge iOS and Android, and then enter a different page. For example, Baidu map is like this, and desktop devices access ghost. This is how the user agent works.

I didn't go into details about how Baidu map judges desktop devices and mobile devices, but I want to find a comprehensive user agent code. I don't know if you know there is a website named html5test. Its function is to determine the degree of HTML5 support your browser. However, it also provides browser and device information. On the desktop, I tried Firefox, chrome, ie, Safari, and other tests correctly. on mobile devices, I tested safari, opera Mini, and UC on the Apple iPod, in addition to the unidentifiable UC, all are OK. In addition, the Nokia e63 is also tested. The default browser is OK, and UC cannot be identified.

Since html5test has been done so comprehensively, even browsers, engines, device types, and device names are all available, it means that the user agent should be well judged, at least, it is the best I have seen today. Then we can see that html5test is open-source, so download and analyze it. After reading this, I did not think that html5test was encapsulated so well that a JS contains basically all the user agent judgments. This JS file can be viewed directly here.

I took this JS out separately, added a few more HTML words, and made a browser test page. You can access this page to view your browser information and device information. For more information, see. Address: http://rovertang.com/labs/useragent/

I think, through this JavaScript code, you can get the browser information, device type, and device name. The following is what you do, but there is also a little worry: a js file larger than 50 K seems a little pressure on page loading.

By the way, I found that IE does not support HTML5 doctype labels very well and some tests fail. The Internet Explorer is really annoying.

Finally, I recommend a Firefox component. I think many people have used it, namely, user agent switcher. The function is to disguise it as another browser under ff, such as the iPhone browser, in this way, you can directly browse the mobile web page under ff for code analysis.

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.