Javascript jQuery 1.9浏览器检测

Javascript jQuery 1.9浏览器检测,javascript,jquery,browser-detection,browser-feature-detection,Javascript,Jquery,Browser Detection,Browser Feature Detection,在早期版本中,我曾经测试是否应该在页面加载时手动触发popstate,因为Chrome在加载后立即触发,而Firefox和IE则不会 if ($.browser.mozilla || $.browser.msie) { $(window).trigger('popstate'); } 既然他们在1.9中删除了browser对象,我应该如何测试这些浏览器?或者如何判断是否需要在页面加载时popstate 代码是: $(function(){ $(window).on('popst

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

if ($.browser.mozilla || $.browser.msie) {
    $(window).trigger('popstate');
}
既然他们在1.9中删除了browser对象,我应该如何测试这些浏览器?或者如何判断是否需要在页面加载时
popstate

代码是:

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

    // manual trigger loads template by URL in FF/IE.
    if ($.browser.mozilla || $.browser.msie) {
       $(window).trigger('popstate');
    }
});
更新 他说:

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

我想把这个密码给你就行了。如果您需要,请不要忘记根据您的要求进行更改

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;
var匹配,浏览器;
//不赞成使用jQuery.browser。
//更多详情:http://api.jquery.com/jQuery.browser
//jQuery.uaMatch为后台兼容维护
jQuery.uaMatch=函数(ua){
ua=ua.toLowerCase();
变量匹配=/(chrome)[\/]([\w.]+)/.exec(ua)||
/(webkit)[\/]([\w.]+)/.exec(ua)||
/(opera)(?:.*版本|)[\/]([\w.]+)/.exec(ua)||
/(msie)([\w.]+)/.exec(ua)||
ua.indexOf(“兼容”)<0&/(mozilla)(?:.*rv:([\w.]+)/.exec(ua)||
[];
返回{
浏览器:匹配[1]| |“”,
版本:匹配[2]| |“0”
};
};
matched=jQuery.uaMatch(navigator.userAgent);
浏览器={};
如果(匹配的浏览器){
browser[matched.browser]=true;
browser.version=匹配的.version;
}
//Chrome是Webkit,但Webkit也是Safari。
如果(browser.chrome){
browser.webkit=true;
}else if(browser.webkit){
browser.safari=true;
}
jQuery.browser=浏览器;
供您参考-jQuery文档 我们建议不要使用此属性;请尝试使用此功能 而不是检测(请参见jQuery.support)。jQuery.browser可以移动到 jQuery未来版本中的插件


您应该在
popstate
处理程序中添加一点健全性检查,并确保如果您“弹出”到与开始时相同的状态,它不会产生任何代价。这样,您就不必关心浏览器,只需在document ready上调用您的popstate即可:

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

    // call popstate on document ready
    $(popstate);
});
建议您将
$.browser
中的代码粘贴回您的环境中的答案对于支持错误的做法来说是过分的。你可以检测99%的你需要的东西。几乎每次使用
$.browser
都是危险的。几乎总是有办法检测到这一点

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

我请求您不要将
$复制到您的代码中。浏览器
返回到您的代码中,jQuery团队出于某种原因决定终止它。

是解决此问题的快速方法。 将这行代码添加到jQuery-1.9.js中,并将$.browser替换为jQuery.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());

你能改为监听事件吗?如果它没有在页面加载触发之前发生,请注意Chrome的行为是有缺陷的。虽然规范已经被修正,但根据这一点:我实际上不知道那张罚单在说什么,但我的已经作为副本关闭了:将浏览器中的代码重新插入并不是答案。。。不嗅是一种习惯。另外,请将这段巨大的代码归为属性,或者在github repo中链接到它。这是许可代码。我没有,我实际上做了一些类似于你建议的事情。我更新的问题有我得出的解决方案。此外,我建议将答案与最终解决方案一起发布,而不是编辑你的问题。这一点也不“糟糕的形式”,实际上是鼓励的。我很欣赏这个答案的最后一行。我正要这么做,是真的;“$.browser”已关闭。jquery通过“jquery.support”在内部使用特性检测,但他们强烈建议使用外部库,如Modernizer。