gpt4 book ai didi

javascript - 使用javascript检测用户浏览器

转载 作者:行者123 更新时间:2023-11-30 08:42:10 24 4
gpt4 key购买 nike

我正在使用以下代码来发现用户浏览器:

navigator.appName == "Microsoft Internet Explorer"

一直有效,但 IE11 正在返回 Netscape

我读到浏览器检测是一种不好的做法。 ( Why does JavaScript navigator.appName return Netscape for Safari, Firefox and Chrome? ),我们应该检测特征。但是 site MS 教我如何检测 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

话虽这么说:

了解我必须使用什么的正确方法是什么?

比如我用的是IE,命令是:

window.document.execCommand('Stop');

否则,命令是

window.stop()

搭顺风车,知道浏览器是否支持HTML5的正确方法是什么?

最佳答案

正如您所提到的,正确的方法是只检查该功能,而根本不要进行浏览器嗅探。

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

反过来,你可以用 execCommand 填充 window.stop,就像这样

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

这也将确保它是 native 方法,并在 Chrome、Firefox 和 Opera 中进行了测试

关于javascript - 使用javascript检测用户浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25375318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com