userAgent = navigator.userAgent,//User Agent is called user agent in Chinese, or UA for short. It is a special string header that enables the server to identify the operating system and version, CPU type, browser and version, and browser rendering used by the customer. Engine, browser language, browser plug-in, etc.
The following are the navigator.userAgent of each browser
//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.4506.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
//Regular for matching response browser representation
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 in the following way, but because Like safari, both have "safari". We can put it in front of rwebkit. to match
rchrome=/(Chrome)[ /]([w.] )/
Firefox can also do the same, put it in front of rmozilla for judgment
rfirefox=/(Firefox)[ /]([w.] )/
uaMatch : function(ua) {//Mainly the type and version of the browser obtained
ua = ua.toLowerCase();//The parameter is navigator.userAgent. Convert to lowercase. Of course, you can also declare case-insensitivity in the regular match (such as /(webkit)[ /]([w.] )/i)
var match = rwebkit.exec(ua) | |
ropera.exec( ua ) ||
rmsie.exec( ua ) ||
ua.indexOf("compatible") [] ;//Since exec is used for matching, the returned result is an overall match and a submatch of the response, such as /(Chrome)[ /]([w.] )/, match will return Chrome/13.0.782.220, match[1 ] get chrome, match[2] get 13.0.782.220;
return { browser: match[1] || "", version: match[2] || "0" };
}
//uaMatch is a method in jQuery. Here, start calling and saving the browser model browser and version
browserMatch = jQuery.uaMatch( userAgent ); // Call uaMatch, and Return object return { browser: match[1] || "", version: match[2] || "0" };
if ( browserMatch.browser ) {
jQuery.browser[ browserMatch.browser ] = true;//chromesafarioperamsiemozilla, for example, when we want to perform a specific operation under the opera browser, we can use $.browser.opera to determine whether it is the opera browser (true, false)
jQuery.browser.version = browserMatch.version;//Browser version
}
if ( jQuery.browser.webkit ) {
jQuery.browser.safari = true;//Because safari uses AppleWebKit The engine, since Safari and Chrome both have it, are being processed separately
}