简体   繁体   English

从我的网站阻止某些设备和浏览器

[英]Blocking certain devices and browsers from my website

从我的网站阻止旧版本的浏览器和移动设备的最佳方法是什么(阻止他们(向他们发送消息以了解该网站兼容的浏览器))?

The best way is to use feature detection to detect whether the features you require are supported by the browser. 最好的方法是使用功能检测来检测浏览器是否支持所需的功能。 The library modernizr contains a wealth of feature detection logic that you can either use from the library or you can borrow individual elements from and copy into your code. modernizr包含大量的功能检测逻辑,您可以从库中使用它们,也可以从中借用单个元素并将其复制到代码中。 The reason this is the best way is because you aren't stuck with tracking and keeping up with individual versions of individual browsers and you aren't relying on browser type and version identification which is not guaranteed to be accurate and can be explicitly set by users to spoof. 这是最好的方法,是因为您不必跟踪和跟上各个浏览器的各个版本,也不必依赖于不能保证准确的浏览器类型和版本标识,并且可以通过以下方式明确设置:用户欺骗。 With feature detection, either the host browser has a capability you need or it doesn't and this is figured out at run-time. 通过功能检测,主机浏览器具有您需要的功能,也可以不需要,而可以在运行时确定。 If your site is not usable on small screens, you can use feature detection to test for window size. 如果您的站点无法在小屏幕上使用,则可以使用功能检测来测试窗口大小。

Another way (but certainly not the best way) is to detect a browser type and version and inform the user if they are using a version you haven't tested. 另一种方法(但肯定不是最好的方法)是检测浏览器的类型和版本,并通知用户是否使用了未经测试的版本。 You can use the navigator.userAgent property and examine what's in that string to identify various browsers and browser versions. 您可以使用navigator.userAgent属性,并检查该字符串中的内容以标识各种浏览器和浏览器版本。 There's a whole library here that gives you an idea what you can look for. 有一个整个图书馆在这里 ,给你一个想法,你可以寻找什么。

If you're using jQuery, then you can use jQuery.browser and jQuery.browser.version as it has already parsed some of the information from the useragent string. 如果使用的是jQuery,则可以使用jQuery.browserjQuery.browser.version因为它已经解析了useragent字符串中的某些信息。

Anything can be "spoofed" but you could use jquery.browser to do this. 任何东西都可以被“欺骗”,但是您可以使用jquery.browser来做到这一点。 Find more info here: jQuery 在此处查找更多信息: jQuery

A few conditional if else if statements could provide an error message that their browser is not compatible. 一些条件if语句可以提供错误消息,表明其浏览器不兼容。 Again this would not be 100% fool proof, but if you are trying to block users that are still using really old browsers, I would think it would be pretty effective. 同样,这并不是100%的傻瓜证明,但是如果您试图阻止仍在使用旧版浏览器的用户,我认为这将非常有效。

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

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