jQuery 1.9浏览器检测 [英] jQuery 1.9 browser detection

查看:180
本文介绍了jQuery 1.9浏览器检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In earlier versions, I used to test if I should be triggering popstate manually on page load, because Chrome triggers it right after load, and Firefox and IE do not.

if ($.browser.mozilla || $.browser.msie) {
    $(window).trigger('popstate');
}

现在他们将浏览器对象丢弃在1.9中,我应该如何测试这些浏览器?或者如何在页面加载时确定是否需要 popstate

Now that they dropped the browser object in 1.9, how should I test for these browsers? Or how do I figure if I need to popstate on page load or not?

代码为:

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

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



更新



去了这个:

Update

Went for this:

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


推荐答案

你应该加一点理智检查你的 popstate 处理程序,如果你弹出到你开始的相同状态,请确保它没有做任何昂贵的事情。然后你不关心浏览器,而只是在文档就绪时调用你的popstate:

You should add a little sanity check to your popstate handler, and make sure that it doesn't do anything expensive if you "pop" into the same state you started in. Then you can not care about the browser, and instead just call your popstate on document ready:

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

    // call popstate on document ready
    $(popstate);
});

答案建议您粘贴 $。浏览器回到你的环境是一种矫枉过正的做法,以支持不良做法。您可以检测99%的所需内容。几乎每次使用 $。browser 都是危险的。 几乎总是可以检测到它。

The answer suggesting you paste the code from $.browser back into your environment is way overkill to support a bad practice. You can feature detect 99% of the things you need to. Almost every use of $.browser is a dangerous. There are almost always ways to detect that.

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

The JavaScript community has been against browser sniffing for a long time. Here is a post from 2009 telling us why it's a bad idea. There are many others.

我请求你不要将 $。browser 复制回你的代码,jQuery团队决定以某种原因杀死它。

I beg you not to copy $.browser back into your code, the jQuery team decided to kill it for a reason.

这篇关于jQuery 1.9浏览器检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆