gpt4 book ai didi

javascript - 使用 Chrome 检测浏览器

转载 作者:行者123 更新时间:2023-11-28 16:14:25 24 4
gpt4 key购买 nike

我有这个代码:

<body onload="detect(navigator.appName)">
<h2 id=alert>This tutorial is for Google Chrome users,
why would you want to read it?</h2>
</body>
<script>
function detect(x){
alert(x)
if (x != "Chrome"){
document.getElementById("alert").style.display = '';
}
}
</script>

问题是,每当我使用 Google Chrome 打开页面时,它都会返回“Netscape”。有解决办法吗?

Webpage

最佳答案

在 Chrome 上,这是 navigator.appName 将为您提供的内容:

Netscape

如果你解析用户代理会容易得多

if ( ! /(Chrome)/i.test(navigator.userAgent)) {
document.getElementById("alert").style.display = '';
}

但我不得不说,如果您只向那些使用 Chrome 的人展示您的教程,那么您就错过了互联网的意义。

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

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