8

How to check if the user disabled javascript in his browser? For example: the stackoverflow site shows a red warning div at the top if you disable the javascript in firefox? How to achieve this?

One more thing I noticed: if you disable cookies, stackoverflow doesn't work. It won't let you login when you click the login button. Actually, that's what I assume with my website as well, the user login/session data depends on cookies, UI layout etc.. depends on javascript.

Here I don't want to discuss about the design principle (progressive enhancement etc..), could anyone tell me a good way to check if cookies, javascript are enabled or not? I think it has to be done on server side, right?

Thank you

3 Answers 3

18

Add a <noscript> tag to the page, and if JavaScript is disabled, the message will show to the user.

You can also detect if JavaScript is enabled based on the success of running JavaScript code

  • add a hidden field and set its default value to false
  • Run some javascript code that sets the value to true
  • When you post back, check the value.

To check for cookies, set a cookie and try to read it. If it succeeds, then cookies are enabled.

Sign up to request clarification or add additional context in comments.

2 Comments

to be more specific, use <noscript> for javascript disabled warning; if js is enabled, use navigator.cookieEnabled to check if cookies is enabled from client side
Just be aware, rendering of noscript only happens when the browser does not support JavaScript if the user has disabled it. It will not be rendered if a JavaScript error causes JS execution to halt.
3
<!-- gatewayPage.php -->
<noscript>
    <div style="background-color:red;font-size:14pt;">
        This site will be very boring unless you enable Javascript!
    </div>
    <!-- if you're convinced that bots won't follow your redirect, add this: -->
    Proceed to <a href="myRealHomePage.php">MySite</a> anyway
</noscript>
<script>
    documment.location = "myRealHomePage.php";
</script>

4 Comments

I just hope the search spider follows JavaScript redirects.
this is bad for SEO, as the search engine crawlers cannot follow javascript redirects.
Maybe once upon a time, but googlebot definitely follows mine.
Added "real" link, just in case.
0

Use the noscript tag:

<script>
document.write("JS is enabled!");
</script>
<noscript>JS is disabled !</noscript>

source : http://www.w3schools.com/tags/tag_noscript.asp

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.