Ruby on Rails检查是否启用了javascript

时间:2013-03-10 14:50:23

标签: javascript ruby-on-rails

如果用户在浏览器中启用了javascript,我该如何检查我的布局代码? 如果未启用javascript,我想显示错误页面。例如。 404或500错误页面。我怎么能用Rails做到这一点?

1 个答案:

答案 0 :(得分:7)

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

但是,您只需显示<noscript>标记的消息,如:

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

大多数浏览器默认启用了javascript,因此这是一个不常见的问题。如果您愿意,可以在link_to中添加<noscript>,以便向他们展示如何激活这样的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>