Copy codeThe Code is as follows:
// Useragent RegExp
Rwebkit =/(webkit) [\/] ([\ w.] + )/,
Ropera =/(opera )(? :. * Version )? [\/] ([\ W.] + )/,
Rmsie =/(msie) ([\ w.] + )/,
Rmozilla =/(mozilla )(? :.*? Rv :( [\ w.] + ))? /,
// Keep a UserAgent string for use with jQuery. browser
UserAgent = navigator. userAgent,
// For matching the engine and version of the browser
BrowserMatch,
// Use of jQuery. browser is frowned.
// More details: http://docs.jquery.com/Utilities/jQuery.browser
UaMatch: function (ua ){
Ua = ua. toLowerCase ();
Var match = rwebkit.exe c (ua) |
Ropera.exe c (ua) |
Rmsie.exe c (ua) |
Ua. indexOf ("compatible") <0 & rmozilla.exe c (ua) |
[];
Return {browser: match [1] | "", version: match [2] | "0 "};
}
BrowserMatch = jQuery. uaMatch (userAgent );
If (browserMatch. browser ){
JQuery. browser [browserMatch. browser] = true;
JQuery. browser. version = browserMatch. version;
}
Analysis: compatibility issues between browsers, a historical issue. The pain point is that a front-end developer has to pay for this compatibility issue. During development, we often need to make partial code adjustments for each browser to run properly in the corresponding browser. I know two methods: browser detection and object detection.
In jQuery, you only need to use the row uaMatch () function. Key points used are: 1.regExp.exe c (str); 2. navigator. userAgent;
1. regexp.exe c (str); use a regular expression to search for the string and return an array. The 0 element of this array is the full regular match, and the 1 element is the first child match, the 2 element is the second child match, and so on.
2. navigator. userAgent: one of the important objects in the javascript BOM. It is mainly used to return information about the current browser and operating system.
For example: firefox navigator. userAgetn string: "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv: 1.9.2.13) Gecko/20101203 Firefox/3.6.13"
Navigator of IE8. userAgetn string: "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0 ;. net clr 1.1.4322 ;. net clr 2.0.50727 ;. net clr 3.0.20.6.2152 ;. net clr 3.5.30729 )".
According to the Regular Expression of Each browser, the first child matches the name of the browser, and the second is the version.