jQuery 1.9浏览器检测

bev*_*qua 19 javascript jquery browser-detection browser-feature-detection

在早期版本中,我曾经测试过我是否应该popstate在页面加载时手动触发,因为Chrome会在加载后立即触发,而Firefox和IE则不会.

if ($.browser.mozilla || $.browser.msie) {
    $(window).trigger('popstate');
}
Run Code Online (Sandbox Code Playgroud)

现在他们在1.9中删除了浏览器对象,我应该如何测试这些浏览器?或者我如何计算是否需要popstate页面加载?

代码是:

$(function(){
    $(window).on('popstate', popState);

    // manual trigger loads template by URL in FF/IE.
    if ($.browser.mozilla || $.browser.msie) {
       $(window).trigger('popstate');
    }
});
Run Code Online (Sandbox Code Playgroud)

更新

去了这个:

    function popState(e){
        var initial = e.originalEvent === undefined || e.originalEvent.state === null;
        if(!initial){
            activateRoute({
                key: e.originalEvent.state.key,
                settings: e.originalEvent.state.settings
            },'replace');
        }
    }

    function init(){
        $(window).on('popstate', popState);

        $(function(){
            var route = getRoute(document.location.pathname);
            activateRoute(route, 'replace');
        });
    }
Run Code Online (Sandbox Code Playgroud)

gna*_*arf 21

你应该给你的popstate处理程序添加一点健全性检查,如果你"弹出"到你开始的相同状态,确保它没有做任何昂贵的事情.那么你不关心浏览器,而只是打电话给你的popstate准备好文件:

$(function(){
    $(window).on('popstate', popState);

    // call popstate on document ready
    $(popstate);
});
Run Code Online (Sandbox Code Playgroud)

答案表明您将代码粘贴$.browser回您的环境中是一种过度杀戮以支持不良做法.您可以检测99%的所需内容.几乎每次使用$.browser都是危险的.有几乎总有办法检测.

JavaScript社区长期以来一直反对浏览器嗅探. 是2009年的一篇文章,告诉我们为什么这是一个坏主意.还有很多其他的.

我请求你不要复制$.browser回你的代码,jQuery团队决定杀死它是有原因的.


Aza*_*ade 7

是解决这个问题的快捷方法.将这行代码添加到jQuery-1.9.js并用jQuery.browser替换$ .browser

jQuery.browser = {};
jQuery.browser.mozilla = /mozilla/.test(navigator.userAgent.toLowerCase()) && !/webkit    /.test(navigator.userAgent.toLowerCase());
jQuery.browser.webkit = /webkit/.test(navigator.userAgent.toLowerCase());
jQuery.browser.opera = /opera/.test(navigator.userAgent.toLowerCase());
jQuery.browser.msie = /msie/.test(navigator.userAgent.toLowerCase());
Run Code Online (Sandbox Code Playgroud)

这里


Tec*_*hie 5

我想把这段代码放到你身边就可以了.如果您需要,请不要忘记进行更改.

var matched, browser;

// Use of jQuery.browser is frowned upon.
// More details: http://api.jquery.com/jQuery.browser
// jQuery.uaMatch maintained for back-compat
jQuery.uaMatch = function( ua ) {
    ua = ua.toLowerCase();

    var match = /(chrome)[ \/]([\w.]+)/.exec( ua ) ||
        /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
        /(opera)(?:.*version|)[ \/]([\w.]+)/.exec( ua ) ||
        /(msie) ([\w.]+)/.exec( ua ) ||
        ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec( ua ) ||
        [];

    return {
        browser: match[ 1 ] || "",
        version: match[ 2 ] || "0"
    };
};

matched = jQuery.uaMatch( navigator.userAgent );
browser = {};

if ( matched.browser ) {
    browser[ matched.browser ] = true;
    browser.version = matched.version;
}

// Chrome is Webkit, but Webkit is also Safari.
if ( browser.chrome ) {
    browser.webkit = true;
} else if ( browser.webkit ) {
    browser.safari = true;
}

jQuery.browser = browser;
Run Code Online (Sandbox Code Playgroud)

为了您的信息 - jQuery文档

我们建议不要使用此属性; 请尝试使用功能检测(请参阅jQuery.support).jQuery.browser可能会在未来的jQuery版本中移动到插件中.

jQuery.browser

jQuery.support

  • 从浏览器中插入代码不是答案......不是嗅探.另外,请将这一大块代码归结为github repo中的链接.这是许可代码. (6认同)