JQUERY1.6 Method 4: browser Detection

Source: Internet
Author: User

UserAgent = navigator. userAgent, // The Chinese name of the User Agent is the User Agent (UA), which is a special string header, the server can identify the operating system and version, CPU type, browser and version, browser rendering engine, browser language, and browser plug-in used by the customer.

The navigator. userAgent of Each browser is as follows:

// Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1 -- safari

// Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.220 Safari/535.1 -- chrome

// Opera/9.80 (Windows NT 5.1; U; Edition Next; zh-cn) Presto/2.8.158 Version/11.50 -- opera

// Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1 ;. net clr 2.0.50727 ;. net clr 3.0.20.6.2152 ;. net clr 3.5.30729; InfoPath.1 ;. NET4.0C ;. NET4.0E; InfoPath.2) --- ie

// Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv: 1.9.2.24) Gecko/20111103 Firefox/3.6.24 -- firefox



// Match the regular expression in the response Browser

Rwebkit =/(webkit) [\/] ([\ w.] +)/, // webkit
Ropera =/(opera )(? :. * Version )? [\/] ([\ W.] +)/, // opera
Rmsie =/(msie) ([\ w.] +)/, // ie
Rmozilla =/(mozilla )(? :.*? Rv :( [\ w.] + ))? //, // Mozilla



Through userAgent, we can see that chrome can be matched through the following method, but because like safari, both have "safari", we can put it before rwebkit.

Rchrome =/(Chrome) [\/] ([\ w.] + )/

Firefox can also do the same and put it before rmozilla for judgment.

Rfirefox =/(Firefox) [\/] ([\ w.] + )/



UaMatch: function (ua) {// mainly obtains the type and version of the browser.
Ua = ua. toLowerCase (); // The parameter is navigator. userAgent. Converts the string to lowercase. Of course, you can declare the string match to be case insensitive (for example,/(webkit) [\/] ([\ w.] +)/I)

Var match = rwebkit.exe c (ua) |
Ropera.exe c (ua) |
Rmsie.exe c (ua) |
Ua. indexOf ("compatible") <0 & rmozilla.exe c (ua) |
[]; // Because exec is used for matching, the returned result is a full match and a submatch of the response, for example,/(Chrome) [\/] ([\ w.] +)/, match will return Chrome/13.0.782.220, match [1] to get chrome, match [2] to get 13.0.782.220;

Return {browser: match [1] | "", version: match [2] | "0 "};
}

// UaMatch is a method in jQuery. You can call and save the browser Model browser and version here.

BrowserMatch = jQuery. uaMatch (userAgent); // call uaMatch and return the object return {browser: match [1] | "", version: match [2] | "0 "};
If (browserMatch. browser ){
JQuery. browser [browserMatch. browser] = true; // chrome \ safari \ opera \ msie \ mozilla. browser. opera to determine whether it is an operabrowser (true, false)

JQuery. browser. version = browserMatch. version; // browser version
}


If (jQuery. browser. webkit ){
JQuery. browser. safari = true; // because safari uses the AppleWebKit engine, because both safari and chrome have safari, we are doing additional processing.
}

Related Article

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.