5

I want to check whether javascript and cookies are enabled or not on page load in Ruby on Rails. So if anyone tries to open it, website should display a message that he cannot proceed further.

simonmenke
  • 2,819
  • 19
  • 28
Sonal S.
  • 1,444
  • 1
  • 15
  • 31

3 Answers3

3

You coud use the <noscript> ... </noscript> tag. For more information look here.

simonmenke
  • 2,819
  • 19
  • 28
0

If you need to know this server side, you can set a cookie through javascript. If rails can't read it, than the user doesn't have cookies or javascript enabled.

vise
  • 12,713
  • 11
  • 52
  • 64
0

The <noscript> tag does not seem to work with script blockers like chrome ScriptSafe. For this case I have found a workaround: create an alert message that is removed, in case javascript works well.

This is the alert message with id="noscriptmessage":

<div id="noscriptmessage" style="color:red">
   This site requires javascript enabled!
</div>

And the following script will remove the message, if javascript is enabled and not blocked:

<script>
document.getElementById("noscriptmessage").innerHTML="";
</script>

Make sure that the script is found after the message (e.g., if you place the script in the header before the alert message, it will not work).

Olli
  • 1,621
  • 15
  • 18
  • Sorry, I had missed the hint that this question was duplicate: see: [link](http://stackoverflow.com/questions/2315648/check-if-javascript-is-enabled-serverside-with-rails). There user "bobince" has described the same solution. You might want to check out that page, together with a ton of other possible solutions. – Olli Jan 28 '15 at 05:08