[JQuery] Determining the browser type and version

Source: Internet
Author: User

[JQuery] Determining the browser type and version

This is a companion article with [HTML] writing different HTML code based on different browser types (click to open the link). IE annotations can help you based on different versions of HTML on the webpage, render different codes.

This article uses jQuery to determine the browser type and version and execute different Javascript scripts in the script section.

Because the code for directly using Javascript to determine the browser version is complicated, Javascript does not have an encapsulated interface. Jquery has an encapsulated interface and does not need to write so much code, so it is implemented directly using Jquery.

But it is worth noting that jQuery has removed $. browser and $. browser. version from version 1.9 and replaced it with $. support. In the latest version 2.0, IE 6/7/8 is no longer supported. Later, if you need to support IE 6/7/8, you can only use jQuery 1.9. That is to say, the following judgment is only applicable to jQuery 1.9, but not even jQuery 1.10.

The Code is as follows:

 

<Script type = text/javascript> if ($. browser. msie) {alert (IE browser); if ($. browser. version <8) {alert (IE7 and below);} else {alert (IE8 and above);} else if ($. browser. webkit) {alert (Apple's Safari, Google's Chrome browser);} else if ($. browser. mozilla) {alert (FireFox browser);} else if ($. browser. opera) {alert (operabrowser) ;}</script>

As only a few bullet boxes will not be displayed.

 

$. Browser. version is used here. It is also important to judge IE7 and non-IE7, because some code above IE7 is really different from the following implementation ......

For Versions later than jQuery 1.9, because $. browser is removed, you can use a regular expression to define $. browser. version:

 

$.browser.mozilla = /firefox/.test(navigator.userAgent.toLowerCase());$.browser.webkit = /webkit/.test(navigator.userAgent.toLowerCase());$.browser.opera = /opera/.test(navigator.userAgent.toLowerCase());$.browser.msie = /msie/.test(navigator.userAgent.toLowerCase());

Then, use the attributes that IE6 does not have to determine IE6:

 

 

('undefined' == typeof(document.body.style.maxHeight)) {}

If IE8 is determined, leadingWhitespace is used. LeadingWhitespace: If the browser retains leading white spaces when using innerHTML, true is returned. Currently, false is returned in IE 6-8.

 

 

if (!$.support.leadingWhitespace) {}

Directly using Javascript without Jquery is more complicated, mainly because the character attributes of navigator. appName and navigator. appVersion are cut using the string method to judge. No code is posted here. Interested in viewing different browsers, alert (navigator. appName +, + navigator. appVersion); what is it. For example, IE8 is:

 

Then you will use the string method to cut these strings.

 

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.