Copy Code code 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 the Jquery.browser is frowned upon.
More Details:http://docs.jquery.com/utilities/jquery.browser
Uamatch:function (UA) {
UA = Ua.tolowercase ();
var match = rwebkit.exec (UA) | |
Ropera.exec (UA) | |
Rmsie.exec (UA) | |
Ua.indexof ("compatible") < 0 && rmozilla.exec (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: Browser compatibility issues, a legacy of this history. The pain is that as a front-end developer has to pay for this compatibility issue. When we do development, we often have to make local code adjustments for each browser in order to run well in the appropriate browsers. There are two kinds of methods I know: 1. browser detection; 2. Object detection.
In jquery, just use the line Uamatch () function. The key points to be used are: 1.regexp.exec (str); 2.navigator.useragent;
One, regexp.exec (str); Use a regular expression to find in a string and return an array whose 0 elements are the entire regular match, the 1 element is the first child match, and the 2 element is the second child match, and so on.
Two, navigator.useragent: is one of the most important objects of JavaScript BOM, it is used to return the current browser and the information of the operating system mainly.
For example: Firefox's 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 "
IE8 's Navigator.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.4506.2152;. NET CLR 3.5.30729) ".
From the regular expressions in each browser, the first child matches the browser's name and the second version.