我有这个代码:
<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”。有解决办法吗?
最佳答案
在 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/