简体   繁体   English

现代浏览器检测?

[英]Modern browsers detection?

What versions of browsers are actually can be named modern?哪些版本的浏览器实际上可以命名为现代? Where css3 and more or less modern features are supported?哪里支持 css3 和或多或少的现代功能?

  • No idea since when opera become modern不知道什么时候歌剧变得现代了
  • If FF4 can be called modern如果FF4可以称为现代
  • Does Safari less than 5 is that bad? Safari 小于 5 有那么糟糕吗?

thanks!谢谢!

function isOldBrowser() {
    var isOld = false;
    var detect = function(){
        var ua= navigator.userAgent, tem, M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*([\d\.]+)/i) || [];
        if(/trident/i.test(M[1])){
            tem=  /\brv[ :]+(\d+(\.\d+)?)/g.exec(ua) || [];
            return 'IE '+(tem[1] || '');
        }
        M = M[2] ? [M[1].toLowerCase(), parseInt(M[2],10)] : [navigator.appName.toLowerCase(), parseInt(navigator.appVersion,10), '-?'];
        if((tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
        return M;
    };
    var browser = detect();

    if (document.all && !document.querySelector) isOld = true; //IE7 or lower
    else if (document.all && document.querySelector && !document.addEventListener) isOld = true; //IE8
    else if (browser[0] == 'firefox' && browser[1] < 4) isOld = true; //FF4+
    else if (browser[0] == 'safari' && browser[1] < 5) isOld = true;  //Safari 5+
    return isOld;
}

Browser sniffing is almost always a bad idea .浏览器嗅探几乎总是一个坏主意 Use Modernizr and feature detection instead to serve up alternative content if you wish to support older browsers.如果您希望支持旧浏览器,请改用Modernizr和功能检测来提供替代内容。 You can refer to caniuse to find out which features are supported by which browsers, too.您也可以参考caniuse来了解哪些浏览器支持哪些功能。

This works for me:这对我有用:

    var isModernBrowser = typeof(Intl) != "undefined";

The other thing this does is if you're running Internet Explorer 11, but under SharePoint (even with latest SharePoint 2016), it will correctly return false because SharePoint forces the page to document.documentMode == 8.这样做的另一件事是,如果您运行的是 Internet Explorer 11,但在 SharePoint 下(即使使用最新的 SharePoint 2016),它将正确返回 false,因为 SharePoint 强制页面为 document.documentMode == 8。

There is a good read on this by Philip Walton: https://philipwalton.com/articles/loading-polyfills-only-when-needed/菲利普沃尔顿对此有很好的阅读: https : //philipwalton.com/articles/loading-polyfills-only-when-needed/

The easiest way to have your code run immediately for most of you users, yet halt execution until polyfills are loaded for all other users is to structure your site so all code paths have a single entry point.让您的代码对大多数用户立即运行,但在为所有其他用户加载 polyfills 之前停止执行的最简单方法是构建您的站点,以便所有代码路径都有一个入口点。

In the case of the website linked above, here is the function used:在上面链接的网站的情况下,这里是使用的功能:

function browserSupportsAllFeatures() {
  return window.Promise && window.fetch && window.Symbol;
}

In my case, I am using something similar:就我而言,我正在使用类似的东西:

const isModernBrowser = () => {
  return 'fetch' in window &&
  'Promise' in window &&
  'assign' in Object &&
  'keys' in Object
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM