jQuery 1.9 browser detection

大城市里の小女人 提交于 2020-01-09 09:03:11

问题


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

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?

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

回答1:


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

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.

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.

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




回答2:


Here is 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.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




回答3:


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

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.browser

jQuery.support



来源:https://stackoverflow.com/questions/14545023/jquery-1-9-browser-detection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!