Javascript jQuery 1.9 浏览器检测

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14545023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:09:06  来源:igfitidea点击:

jQuery 1.9 browser detection

javascriptjquerybrowser-detectionbrowser-feature-detection

提问by bevacqua

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

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

if ($.browser.mozilla || $.browser.msie) {
    $(window).trigger('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 popstateon page load or not?

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

The code is:

代码是:

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

回答by gnarf

You should add a little sanity check to your popstatehandler, 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:

您应该为您的popstate处理程序添加一点健全性检查,并确保如果您“弹出”到您开始时所处的相同状态,它不会做任何昂贵的事情。然后您就可以不关心浏览器,而只需调用您的 popstate文件准备好:

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

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

The answer suggesting you paste the code from $.browserback 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 $.browseris a dangerous. There are almostalways ways to detect that.

建议您将代码从$.browser回粘贴到您的环境中的答案对于支持不良做法来说太过分了。您可以通过功能检测 99% 的您需要的东西。几乎每一次使用$.browser都是危险的。有几乎总有办法检测。

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

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

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

我求求你不要复制$.browser回你的代码,jQuery 团队出于某种原因决定终止它。

回答by Azade

Hereis a fast way to solve this problem. Add this line of codes to your jQuery-1.9.js and replace $.browser with jQuery.browser

是解决此问题的快速方法。将此行代码添加到您的 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());

here

这里

回答by Techie

I guess putting this code would do the trick for you. Don't forget to make changes if you need as per your requirement.

我想把这段代码放在你身上。如果您需要,请不要忘记根据您的要求进行更改。

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;

For your information - jQuery Docs

供您参考 - jQuery 文档

We recommend against using this property; please try to use feature detection instead (see jQuery.support). jQuery.browser may be moved to a plugin in a future release of jQuery.

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

jQuery.browser

jQuery 浏览器

jQuery.support

jQuery.support