简体   繁体   English

使用javascript检测用户浏览器

[英]Detect the user browser with javascript

I am using the following code to discover the user browser: 我正在使用以下代码来发现用户浏览器:

navigator.appName == "Microsoft Internet Explorer"

It always worked, but IE11 is returning Netscape 它始终有效,但是IE11返回Netscape

I have read that Browser detection is a bad practice. 我读过,浏览器检测是一种不好的做法。 ( Why does JavaScript navigator.appName return Netscape for Safari, Firefox and Chrome? ), and we should detect feature. 为什么JavaScript navigator.appName为Safari,Firefox和Chrome返回Netscape? ),我们应该检测到该功能。 But the site of MS is teaching me how to detect the IE browser. 但是MS 网站正在教我如何检测IE浏览器。

In the IE11, even the userAgent metions IE: 在IE11中,即使是userAgent子元素IE:

Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; InfoPath.3; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; rv:11.0) like Gecko Mozilla / 5.0(Windows NT 6.3; WOW64; Trident / 7.0; .NET4.0E; .NET4.0C; InfoPath.3; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; rv :11.0)像壁虎

This being said: 这么说:

What is the right way to know what I have to use? 什么是知道我必须使用的正确方法?

For example, if I am using IE, the command is: 例如,如果我使用的是IE,则命令为:

window.document.execCommand('Stop');

else, the command is 否则,命令是

window.stop()

Taking a ride, what is the correct way to know if the browser supports HTML5? 搭便车,知道浏览器是否支持HTML5的正确方法是什么?

The correct way would be to just check for the feature, as you've mentioned, and never do browser sniffing at all. 正确的方法将是仅检查功能,如前所述,根本不要进行浏览器嗅探。

function stop() {
    if ('execCommand' in document) {
       document.execCommand('Stop');
    }else{
       window.stop()
    }
}

To do it the other way around, you could just polyfill window.stop with execCommand, like this 要做到这一点,您可以使用execCommand来polyfill window.stop,就像这样

if (! ( typeof window.stop == 'function' && 
        window.stop.toString().indexOf('native code') != -1
      )
) {
    window.stop = function () {
        document.execCommand('Stop');
    }
}

That would also make sure it's the native method, and is tested in Chrome, Firefox and Opera 这也将确保它是本机方法,并且已在Chrome,Firefox和Opera中进行了测试

Well wouldn't it be cool if there was a JavaScript library that detects HTML5 and CSS3 features in the user's browser so that you could know if a certain feature is supported instead of relying on browser detection (version nightmares ahead)... 好吧,如果有一个JavaScript库可以检测用户浏览器中的HTML5和CSS3功能 ,那么就可以了,这样您就可以知道是否支持某个功能,而不是依赖浏览器检测(前面的版本噩梦)...

There is... modernizr 有... modernizr

From the Docs : 文档中


Why use Modernizr? 为什么要使用Modernizr? Taking advantage of cool new web technologies is great fun, until you have to support browsers that lag behind. 在您必须支持落后的浏览器之前,充分利用酷炫的新Web技术非常有趣。 Modernizr makes it easy for you to write conditional JavaScript and CSS to handle each situation, whether a browser supports a feature or not. 无论浏览器是否支持某个功能,Modernizr均可让您轻松编写条件JavaScript和CSS来处理每种情况。 It's perfect for doing progressive enhancement easily. 非常适合轻松进行渐进增强。

How it works Modernizr runs quickly on page load to detect features; 工作原理 Modernizr在页面加载时快速运行以检测功能。 it then creates a JavaScript object with the results, and adds classes to the html element for you to key your CSS on. 然后使用结果创建一个JavaScript对象,并将类添加到html元素中,以供您键入CSS。 Modernizr supports dozens of tests, and optionally includes YepNope.js for conditional loading of external .js and .css resources. Modernizr支持数十种测试,并可选地包括YepNope.js,用于有条件地加载外部.js和.css资源。

An Example (also from the docs) 一个例子(同样来自文档)

Modernizr.load({
  test: Modernizr.geolocation,
  yep : 'geo.js',
  nope: 'geo-polyfill.js'
});

In this example, we decided that we should load a different script depending on whether geolocation is supported in the host browser or not. 在此示例中,我们决定根据主机浏览器是否支持地理位置来加载其他脚本。 By doing this, you save users from having to download code that their browser does not need. 这样,您可以避免用户下载其浏览器不需要的代码。 This increases page performance, and offers a clear place to build a healthy amount of abstraction to your polyfills (both 'geo.js' and 'geo-polyfill.js' would seem the same to the rest of the app, even if they're implemented differently). 这样可以提高页面性能,并为在您的polyfill中建立一个健康的抽象空间提供了明确的位置(“ geo.js”和“ geo-polyfill.js”在应用程序的其余部分看起来都是相同的,即使它们“重新实施)。

IMO modernizr is the de facto feature detection JavaScript Library and I use it on ALL my projects. IMO modernizr是事实上的功能检测JavaScript库,我在所有项目中都使用它。 It does have a slight learning curve, but the docs are great, and cover pretty much every feature, and the flexibility it provides you within the above example alone is enough of a reason to use it. 它确实有一点学习曲线,但是文档很棒,并且涵盖了几乎所有功能,并且仅在上面的示例中为您提供的灵活性就足以使用它了。

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

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