jQuery源码分析8: 浏览器检测jQuery.uaMatch

来源:互联网 发布:python 执行 ssh 编辑:程序博客网 时间:2024/06/06 23:09
jQuery源码分析: 浏览器检测jQuery.uaMatch

// Useragent RegExp
var 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,

     browser = {},

      // For matching the engine and version of the browser
      browserMatch;

uaMatch: function( ua ) {  //< ua = navigator.userAgent
    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;
}

// Deprecated, use jQuery.browser.webkit instead
if ( jQuery.browser.webkit ) {
    jQuery.browser.safari = true;
}
原创粉丝点击