检查浏览器名称和版本的简单方法
有没有非常简单的方法来了解浏览器调用了控制器的操作。
我想要我的控制器的代码,如下所示:
if mighty_lib.browser.name == "Safari"
if mighty_lib.browser.version >= 5
# Glad to see you, Safari 5, there is a stunning, interactive page for you.
elsif mighty_lib.browser.version >= 4
# Glad to see you, Safari 4, there is a less advanced page for you.
end
if mighty_lib.browser.name == "Firefox"
# Quick brown fox jumps over the lazy dog
elsif mighty_lib.browser.name == "IE"
# Oh, my god ... Change your browser ! There is a "Jurassic Park" movie for you
end
Is there any really easy way to get to know, what browser has called controller's action.
I would like to have a code for my controller, like this:
if mighty_lib.browser.name == "Safari"
if mighty_lib.browser.version >= 5
# Glad to see you, Safari 5, there is a stunning, interactive page for you.
elsif mighty_lib.browser.version >= 4
# Glad to see you, Safari 4, there is a less advanced page for you.
end
if mighty_lib.browser.name == "Firefox"
# Quick brown fox jumps over the lazy dog
elsif mighty_lib.browser.name == "IE"
# Oh, my god ... Change your browser ! There is a "Jurassic Park" movie for you
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议服务器不是处理不同浏览器要求的最佳位置 - 通常,使用智能 css 和(不引人注目的)javascript 根据浏览器的特定功能在客户端打开和关闭功能是更好的方法客户端而不是修复特定版本。
I would suggest that the server is not the best place to handle different browser requirements - it is generally a much better approach to use smart css and (unobtrusive) javascript to turn features on and off on the client-side according to the specific capabilities of the client as opposed to fixing on specific versions.
我找到了两个可以完成这项工作的 gem:
https://github.com/kevinelliott/agent_orange
https://github.com/visionmedia/user-agent
I found two gem that can make the work:
https://github.com/kevinelliott/agent_orange
https://github.com/visionmedia/user-agent
您可以使用这个旧插件,或者编写一些使用
request.env["的原始代码HTTP_USER_AGENT"]
。另外,请查看 Ruby 论坛 上的评论。You may use this old plugin, or write some raw code that uses
request.env["HTTP_USER_AGENT"]
. Also, take a look at this comment at Ruby forums.