How can I check in my layout code if the user has enabled javascript in his browser? If javascript is not enabled I want to show an error page. E.g. a 404 or a 500 error page. How can I do this with Rails?
Asked
Active
Viewed 4,949 times
3
-
http://stackoverflow.com/questions/13157135/check-whether-javascript-and-cookies-are-enabled-in-ruby-on-rails – Ivaylo Strandjev Mar 10 '13 at 14:56
1 Answers
8
I'm not sure if checking for javascript within Rails is ideal. I found this if you really do want to do it: Check if Javascript is Enabled (Serverside) with Rails
However, you can just show a message with <noscript>
tag like:
<noscript>YOU DON'T HAVE JAVASCRIPT AND THAT OFFENDS ME</noscript>
Most browsers have javascript enabled by default so this is an uncommon problem. If you want you can include a link_to
within the <noscript>
in order to show them how to activate javascript like this:
<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" %>
EDIT
I just found this. I haven't tried it but it looks like exactly what you're looking for: 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>