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

前端 未结 3 875
太阳男子
太阳男子 2021-01-13 06:08

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 cann

相关标签:
3条回答
  • 2021-01-13 06:35

    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.

    0 讨论(0)
  • 2021-01-13 06:43

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

    0 讨论(0)
  • 2021-01-13 06:43

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

    0 讨论(0)
提交回复
热议问题