现在的位置: 首页 > 综合 > 正文

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

2019年10月30日 ⁄ 综合 ⁄ 共 864字 ⁄ 字号 评论关闭

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;
}

抱歉!评论已关闭.