检测jQuery不支持的浏览器(2) [英] Detecting browsers that are not supported by jQuery (2)

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

问题描述

请原谅我这个问题的答案,但我一直找不到.我正在考虑切换到jQuery 2,尽管我不担心支持较旧的浏览器,但我希望能够告诉使用不受支持的浏览器的用户无法使用该网站.

Forgive me if there is an obvious answer to this question, but I haven't been able to find it. I am considering switching over to jQuery 2 and, although I'm not concerned about supporting older browsers, I would like to be able to tell users with unsupported browsers that they can't use the site.

我在这里看到( http://blog.jquery. com/2013/03/01/jquery-2-0-beta-2-released/),您可以使用条件注释在不同版本的IE上分支.js文件,但我相信jQuery 2.0会放弃对还有很多其他浏览器,而不仅仅是IE,因此我认为这不能单独完成[ edit:,这是错误的,请参见下面的较大修改].

I see here (http://blog.jquery.com/2013/03/01/jquery-2-0-beta-2-released/) that you can use conditional comments to branch .js files on different versions of IE, but I believe jQuery 2.0 drops support for a number of other browsers too, not just IE, so I don't think that would do it alone [edit: this is wrong, see larger edit below].

在理想的世界中,我将切换到jQuery 2,然后只有一个javascript函数,当jQuery告诉我它不支持浏览器时会调用该函数.有没有一种我想念的简单方法?

In an ideal world, I'd switch to jQuery 2 and then have a single javascript function that is called when jQuery tells me that it doesn't support the browser. Is there a straightforward way of doing this that I'm missing?

谢谢.

我遇到了这篇指导我的帖子( http://bugs.jquery.com/ticket/13404 )此处: http://jquery.com/browser-support/.事实证明,就支持而言,jQuery 2仅与IE浏览器上的jQuery 1.9不同.因此,也许要问的一个更好的问题是如何检测jQuery不支持的浏览器(通常不只是版本2)-我已经更新了问题标题.

I came across this post (http://bugs.jquery.com/ticket/13404) which directed me here: http://jquery.com/browser-support/. It turns out that, in terms of support, jQuery 2 only differs from jQuery 1.9 on IE browsers. Accordingly, perhaps a better question to ask is how to detect browsers that are not supported by jQuery (in general, not just version 2) - I have updated the question title.

由于功能检测是解决此问题的最推荐方法,因此jQuery支持方法在这里看起来很相关( http://api. jquery.com/jQuery.support/).但是,依靠它似乎也很困难(因为它可能会更改,恕不另行通知).

As feature detection is the most recommended approach to this issue, the jQuery support method looks relevant here (http://api.jquery.com/jQuery.support/). However, it also seems quite iffy to rely on (as it can change without notice).

我想这会提出关键问题. 我应该怎么知道旧浏览器可能不支持哪些jQuery功能?例如,如果某人带着旧版本的4版本复制到该网站Firefox,我不会任何的想法,我需要测试哪些功能.如果jQuery可以提供某种完全受支持的功能测试(例如针对HTML5的测试),那将是王牌,例如: http://html5test.com/

I suppose this creates the key question. How am I supposed to have any idea what jQuery features are or are not subject to potential non-support from old browsers? For instance, if someone comes to the site with a 4-version-old copy of Firefox, I wouldn't have any idea what features I'd need to test for. It would be ace if jQuery could offer some sort of fully-supported feature test, like this for HTML5: http://html5test.com/

好的,因此,使用条件include语句(在下面的答案中以及jQuery的站点上突出显示),您可以处理IE的旧版本.但是,对于其他浏览器来说,这有点棘手.由于您不能依靠jQuery来告诉您有关浏览器对功能x,y或z的支持的任何信息,因此我的方法只是查询基础javascript.如果要查询基于CSS的支持,可以使用modernizr.对于基于JavaScript的支持,这是我用来检测其他浏览器的SUPER旧版本的方法:

Okay, so with the conditional include statements (highlighted in answers below, and on jQuery's site), you can deal with old versions of IE. However, for other browsers, it's a little tricky. Since you cannot rely on jQuery to tell you anything about the browser's support for function x, y, or z, my approach is simply to query the underlying javascript. If you want to query CSS-based support, you can use modernizr. For javascript-based support, this is the method I use to detect SUPER old versions of other browsers:

function browser_ok() {

    if  (
            ( !Array.prototype.indexOf ) ||
            ( !Array.prototype.forEach ) ||
            ( !String.prototype.indexOf ) ||
            ( !String.prototype.trim ) ||                
            ( !Function.prototype.bind ) ||
            ( !Object.keys ) ||
            ( !Object.create ) ||
            ( !JSON ) ||
            ( !JSON.stringify ) ||
            ( !JSON.stringify.length ) ||
            ( JSON.stringify.length < 3 )
        )
    {
        return false;
    }

    // # local storage support
    // source: http://diveintohtml5.info/storage.html

    try {
        var local_storage_support = ( 'localStorage' in window && window['localStorage'] !== null );
        if ( !local_storage_support ) {
            throw new Error("local_storage_support: failed");
        }
    }
    catch ( e ) {
        return false;
    }

    // # AJAX uploads

    if ( !window.FormData || !window.FileReader ) {
        return false;
    }

    // # HTML data elements

    var body = $("body");
    body.data("browser_support_test",42);
    if ( body.data("browser_support_test") !== 42 ) {
        return false;
    }
    else {
        body.removeData("browser_support_test");
    }

    return true;
}

AFAICT,此功能应消除所有可能给jQuery基本功能带来麻烦的浏览器.如果您想做任何花哨的事情,那么您可能知道您需要特定的功能,因此可以对其进行专门检查.

AFAICT, this function should eliminate all browsers that could give jQuery trouble for its basic functionality. If you want to do anything fancy, then there's probably a specific piece of functionality that you know you require, so you can check for it specifically.

推荐答案

它似乎只是放弃了对IE 6、7和8的支持-您应该可以使用:

It looks like it just drops support for IE 6, 7 and 8 - you should be able to use:

HTML:

<!--[if lt IE 9]> <html data-browser="lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html> <!--<![endif]-->

JS:

if( document.documentElement.getAttribute('data-browser') !== null ){
    // not supported by jQuery 2.x
}

如果您决定支持较旧的浏览器,但仍希望jQuery团队在2.x中进行改进,则可以使用1.x版本: http://jquery.com/download/

If you decide to support older browsers but still want the improvements the jQuery team have included in 2.x then you can use the 1.x version: http://jquery.com/download/

更新:

要检测每个不支持jQuery的浏览器非常困难/不切实际,原因是jQuery只是一个JavaScript库.

It's difficult/impractical to detect every browser that doesn't support jQuery, the reason is that jQuery is just a javascript library.

不同的浏览器和版本曾经使用不同的方法来执行javascript中的一些基本操作(例如ajax),这意味着某些旧的浏览器将支持某些功能,而将不支持其他功能. 此浏览器不支持所有功能 ,或者 此浏览器不支持任何功能

Different browsers and versions used to use different methods to do some basic things in javascript (like ajax), this means that some old browsers will support some features and won't support others. There isn't a straight forward this browser supports all features or this browser doesn't support any features test.

Rob W的建议确实很好,可以为现代浏览器提供新版本(2.x),为旧浏览器提供1.x版本(具有旧版支持).

Rob W's suggestion is really good, serve the new version (2.x) to modern browsers and 1.x version (with legacy support) to old browsers.

请查看本文:优美的降级与渐进式增强,并考虑使用<一个href ="http://modernizr.com" rel ="nofollow noreferrer"> Modernizr 库.这样,您可以检查用户浏览器支持哪些功能,并允许您编写应用程序以充分利用最新功能,同时仍为使用旧浏览器的用户提供良好的体验.

Have a look at this article: Graceful degradation versus progressive enhancement and consider using the Modernizr library. This lets you to check which features the user's browser supports and allows you to write your applications to take full advantage of the latest advancements while still providing a good experience for users of older browsers.

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

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