如何检测浏览器类型及其版本
我如何在 Rails 中检测浏览器类型及其版本。我想检查特定浏览器的版本,如果不需要浏览器版本,则要求用户升级它。我使用下面指定的命令,但由于它不遵循标准模式,我无法使用它。
request.env['HTTP_USER_AGENT']
Chrome out put is below
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.205 Safari/534.16
Safari out put is below
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1
FireFox out put is below
Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/20100101 Firefox/4.0
Opera out put is below
Opera/9.80 (Windows NT 5.1; U; en) Presto/2.8.131 Version/11.10
Internet Explorer out put is below
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)
how can i detect browser type and its version in Rails. I want to put check on the version of specific browser and if its not required browser version than ask user to upgrade it.. i use below specified command but as its not following a standard pattern am unable to use it.
request.env['HTTP_USER_AGENT']
Chrome out put is below
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.205 Safari/534.16
Safari out put is below
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1
FireFox out put is below
Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/20100101 Firefox/4.0
Opera out put is below
Opera/9.80 (Windows NT 5.1; U; en) Presto/2.8.131 Version/11.10
Internet Explorer out put is below
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试浏览器 gem。非常简单,可以解决您的目的。
您可以在此处找到非常易于使用的 gem。
Try the browser gem. Very simple and can solve your purpose.
You can find the gem Here very easy to use.
注意:请小心,某些搜索引擎会将其视为一种入侵,请参阅 (Google HTTP USER AGENT)
,或者对于 Firefox
,请在此处检查此内容,您将获得所需的一切
浏览器 Gem
browsers_detection_gem
这是一个可以检测所有浏览器的方法,所以冷静点
NOTE: Be careful some search engines see this as a type of intrusion See ( Google HTTP USER AGENT )
or in the case of firefox
and check this here you will get all you need
browser Gem
browsers_detection_gem
and here is a method which can detect all browsers so chill man
还有 useragent gem:
There is also useragent gem:
你所做的事实际上就是做事的方法。现在,您可以使用正则表达式处理用户代理信息,在 Firefox、Chrome 或您喜欢的任何其他浏览器或版本上查找匹配项。
What you do is actually the way to do it. Now, you can process the user agent information with a regular expression, looking for matches on Firefox, Chrome or any other browser or version you like.
allow_browser (Rails 7.2+)
从
Rails 7.2
开始,有一种内置方法可以检查浏览器类型及其版本。例如:
这里是 相关 PR 的链接 和 文档链接。
allow_browser (Rails 7.2+)
Starting from
Rails 7.2
, there is a built-in way to check browser type and its version.For example:
Here is a link to the relative PR and a link to the docs.
虽然这是一个前端解决方案,但我更喜欢检查所需的特定功能,例如 Flexbox。您可以使用 Modernizr 之类的东西,或者只是一个简单的 JavaScript 检查即可(https://stackoverflow.com/a/27047981/179311)。
Though this is a frontend solution, I prefer to check for a specific feature that is required, such as flexbox. You can use something like Modernizr, or just a simple JavaScript check will do (https://stackoverflow.com/a/27047981/179311).