简体   繁体   English

如何使我的网站检测某些Web浏览器?

[英]How Do I Make My Website Detect Certain Web Browsers?

So I have a website that doesn't work correctly in certain browsers like Firefox. 因此,我有一个网站在某些浏览器(例如Firefox)中无法正常运行。 I was wondering how do I make it so that when someone visits the webpage in an unsupported browser(s) it shows a message. 我想知道如何做到这一点,以便当有人在不受支持的浏览器中访问网页时显示一条消息。 Just like on http://species-in-pieces.com has a page that says "Works Best in Chrome. You Can Download it Here." 就像在http://species-in-pieces.com上一样,页面上显示“在Chrome中效果最佳。您可以在此处下载”。 when you're not using chrome (or something along those lines). 当您不使用chrome(或类似方法)时。 I need a similar message to pop up when on Firefox and not-set browsers (like a gaming console browser) but I also need a way to dissmiss it or ignore it because a big portion of my users are using those specific ones. 在Firefox和未设置的浏览器(例如游戏机浏览器)上时,我需要弹出类似的消息,但我也需要一种方法来关闭它或忽略它,因为我的用户中有很大一部分正在使用这些特定的用户。 Open to any ideas, thanks! 欢迎任何想法,谢谢!

In javascript detecting Firefox: 在检测Firefox的javascript中:

var isFirefox = typeof InstallTrigger !== 'undefined';   // Firefox 1.0+

Read more info and more browser detection: How to detect Safari, Chrome, IE, Firefox and Opera browser? 了解更多信息和更多浏览器检测: 如何检测Safari,Chrome,IE,Firefox和Opera浏览器? .

As a simple example you can do: 作为一个简单的示例,您可以执行以下操作:

var isFirefox = typeof InstallTrigger !== 'undefined';   // Firefox 1.0+
if (isFirefox) {
    alert("Please try other browser!");
}

Example: http://jsfiddle.net/ddan/3d81d3qv/ 示例: http//jsfiddle.net/ddan/3d81d3qv/

You can retrieve the user agent from navigator.userAgent to determine what browser is being used and go from there. 您可以从navigator.userAgent检索用户代理,以确定正在使用的浏览器,然后从那里浏览。

Of course nothing stops the user from faking the user agent. 当然,没有什么可以阻止用户伪造用户代理。

Use 采用

var x = navigator.userAgent;

It will give similar to this: 它将类似于以下内容:

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36

You can block the browser based on this string 您可以根据此字符串阻止浏览器

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

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