最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What's a quick, pure javascript replacement for jquery.browser (removed in jquery 1.9)? - Stack Overflow

programmeradmin0浏览0评论

Yeah, I know that feature detection is preferable.

I've got a case in my codebase where we are using $.browser.msie and $.browser.version to decide whether to render some CSS or not. The developer who wrote the code is no longer with us, and I don't fully understand exactly what kind of feature detection I should write here instead.

As a quick fix, what's the shortest way to implement $.browser.msie and $.browser.version?

Yeah, I know that feature detection is preferable.

I've got a case in my codebase where we are using $.browser.msie and $.browser.version to decide whether to render some CSS or not. The developer who wrote the code is no longer with us, and I don't fully understand exactly what kind of feature detection I should write here instead.

As a quick fix, what's the shortest way to implement $.browser.msie and $.browser.version?

Share Improve this question edited Jan 17, 2013 at 13:44 ripper234 asked Jan 17, 2013 at 12:50 ripper234ripper234 230k280 gold badges645 silver badges914 bronze badges 6
  • 1 Wouldn't the easiest option be to not upgrade jQuery? Then you won't need to change anything at all. – James Allardice Commented Jan 17, 2013 at 12:51
  • Why do you need to detect the browser at all? You could use modernizr instead for feature detection instead of browser detection. – Michał Miszczyszyn Commented Jan 17, 2013 at 12:52
  • @JamesAllardice - we are developing an extension, our clients can choose whatever jQuery version they want. – ripper234 Commented Jan 17, 2013 at 13:01
  • @Miszy - I have a production problem right now, and would rather not think about which features exactly I need, but rather just implement the missing API. – ripper234 Commented Jan 17, 2013 at 13:02
  • 4 @Miszy - There are times when targeting a browser makes sense. For instance, IE8 and below call window.resize when any element on the page is resized. IE9 does not. There's nothing in Modernizr to check for this. The "never use browser detection" dogma reminds me of the "never use html tables" dogma. Let's say "use sparingly, only when truly appropriate" instead. – bergie3000 Commented Jan 17, 2013 at 22:51
 |  Show 1 more comment

4 Answers 4

Reset to default 11

I'll just copy the code from jQuery 1.8.3.

// Limit scope pollution from any deprecated API
(function() {

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

Use conditional comments instead of JavaScript.

<!--[if lte IE 7]>
<link rel="stylesheet" href="ie.css" type="text/css">
<![endif]-->

A quick fix so the script won't break until it's properly refactored (add conditional comments for other versions if needed):

<!--[if IE 7]>
<script> if(typeof $!== typeof undefined){ $.browser = { msie : true, version : 7 }} </script>
<![endif]-->
<!--[if IE 8]>
<script> if(typeof $!== typeof undefined){ $.browser = { msie : true, version : 8 }} </script>
<![endif]-->

JQuery released a Migrate plugin that restores $.browser when used with 1.9. Just be sure to use the production version of Migrate on production servers. The development version will generate a bunch of console messages telling you where your code would break in 1.9 sans Migrate.

Using JQuery Migrate to restore $.browser probably isn't the best long term solution, however. As the name implies, it's intended as a transitional tool, and I don't know that its browser detection functionality will be actively maintained. An alternative would be Modernizr.

What I personally will be doing is keeping my production site on JQ 1.8 for the time being, and testing things out with the development version of Migrate on my dev machine to get a better idea of what 1.9 will break (they did a lot more than just remove $.browser) before deciding on an upgrade strategy.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论