anil_kansal has asked for the wisdom of the Perl Monks concerning the following question:

How can I check whether the user's browser has its JavaScript enabled or not??

Replies are listed 'Best First'.
Re (tilly) 1: Check for JavaScript Enabled
by tilly (Archbishop) on Mar 04, 2002 at 12:37 UTC
    Other people's claims notwithstanding, there is an easy way to arrange it.

    Send back a page that loads fine without JavaScript, and in it include the following:

    <script> document.replace("http://my.company.com/alternate/url"); </script>
    If they have JavaScript then they will be redirected. If not, then they won't.
      This works fine if a user follows the path that you intended, but they don't always do that... say they get the redirected URL from a friend. In order to insure that they did you would have to make sure that they only came from the page doing the redirecting.

      As stated before I would recommend to code a page that sets a cookie in JavaScript and then redirects them to another page. Anyone that sees the cookie has JavaScript enabled.

      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      ()-()                                                      ()-()
       \"/    DON'T   BLAME   ME,   I  VOTED   FOR    PACO!       \"/
        `                                                          ` 
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      
        As mentioned above, give 'em output from a POST form. That way the destination page doesn't show up in the url. More advanced tricks can be done with Apache modules, or with cookies (bad use).

        ____________________
        Jeremy
        I didn't believe in evil until I dated it.

Re: Check for JavaScript Enabled
by Biker (Priest) on Mar 04, 2002 at 09:52 UTC

    You can't. Imagine that you could. There's nothing to stop the visitor to your site to disable JS while your script is busy creating a page assuming that the browser has it enabled.

    You'll have to send down a page with <SCRIPT> and <NOSCRIPT> tags.

    If ever possible, make sure your site/page will work in both cases. If not possible in your case, let the <NOSCRIPT> tag indicate to your visitor that s/he must turn it on to visit and/or make use of your site/page.


    Everything will go worng!

      While i agree entirely that it is probably a silly idea to attempt this you can nonetheless do a test to see if JavaScript is enabled on any given page, although this is almost entirely a JavaScript thing. You can have something in your form like:

      <form name="my_form" method="POST" action="whatever.cgi"> <input type="hidden" name="got_js" value="0" /> <input type="submit" onClick="document.my_form.got_js.value = 1;" /> </form>
      And then in your CGI program you can check to see if got_js is 0 or 1. All of the other possible solutions are variations on this type of thing. But as Biker has pointed out it is probably a silly thing to do - I have only used this on entry to a site in order to provide an alternative to Javascript navigation thingies that the web-designer insisted on using TT2 to provide alternate content if got_js was false.

      /J\