3

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?

Mischa
  • 42,876
  • 8
  • 99
  • 111
Valdis Azamaris
  • 1,433
  • 5
  • 22
  • 47

1 Answers1

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>
Community
  • 1
  • 1
AdamT
  • 6,405
  • 10
  • 49
  • 75