Sonal S.
Sonal S.

Reputation: 1442

Check whether javascript and cookies are enabled in ruby on rails?

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.

Upvotes: 5

Views: 1565

Answers (3)

Olli
Olli

Reputation: 1651

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).

Upvotes: 0

vise
vise

Reputation: 13383

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.

Upvotes: 0

simonmenke
simonmenke

Reputation: 2880

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

Upvotes: 3

Related Questions