2013-03-10 117 views
2

如果用户在他的浏览器中启用了javascript,如何检查我的布局代码? 如果javascript没有启用,我想显示一个错误页面。例如。一个404或500错误页面。我如何用Rails做到这一点?Ruby on Rails检查javascript是否启用

+0

http://stackoverflow.com/questions/13157135/check-whether-javascript-and-cookies-are-enabled在红宝石轨道上 – 2013-03-10 14:56:41

回答

7

我不确定在Rails中检查javascript是否理想。我发现这个,如果你真的想这样做:Check if Javascript is Enabled (Serverside) with Rails

但是,您可以只显示一条消息,<noscript>标签,如:

<noscript>YOU DON'T HAVE JAVASCRIPT AND THAT OFFENDS ME</noscript> 

大多数浏览器都默认启用JavaScript的,所以这是一种不常见的问题。如果你愿意,你可以包括<noscript>link_to为了向他们展示如何执行Javascript这样的:

<noscript>You don't have javascript enabled. Please follow this link for assistance in turning it on. <%= link_to "Turn on javascript", "http://www.enable-javascript.com" %> 

编辑

我刚刚发现这一点。我还没有尝试过,但它看起来像你在寻找什么:Ruby on Rails, Javascript detection

<head> 
    <!-- title, scripts, css etc go here --> 
    <noscript> 
     <meta http-equiv="refresh" content="2;url=http://yoursite.com/nojswarning.html"> 
    </noscript> 
</head> 
+0

谢谢你,伟大的职位,工作完美。 – 2014-03-02 02:50:48