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

Some perl scripts I am running include some javascripts. Is there a perl script that can verify whether javascript is enabled on the user's browser? I would like to set up a conditional statement which checks this. If the return is no, javascripting is not enabled, I don't want the user to continue with the rest of the script. I know I can use a javascript to check this but that would defeat the purpose if the user has disabled javascripting.
Thanks.
Chris.

Replies are listed 'Best First'.
Re: Verifying Javascript is enabled
by ignatz (Vicar) on Jan 23, 2002 at 21:26 UTC
    The simple answer to your question is no. The way to detect for JavaScript is JavaScript. For instance:
    1. Code a page that sets a cookie in javascript and then redirects them to the page with your perl script.
    2. Display your perl functionality only if the cookie is present.
    For more info, see Danny Goodman's Detecting A JavaScript Client

    EDITORIAL
    Personally, this is not something that I would do. I find it much nicer to provide functionality to visitors that don't want to use JavaScript. Most things done with JavaScript are best left to porn websites.

Re: Verifying Javascript is enabled
by ViceRaid (Chaplain) on Jan 23, 2002 at 21:27 UTC

    Basically, no. A Perl script running on the webserver can't query the browser to see whether or not Javascripting is enabled.

    Perhaps the HTML <NOSCRIPT> tag might serve your needs? It contains content that should be displayed by browsers which do not permit Javascript. See the documentation for info on how to make the CGI module set a NOSCRIPT block.

    If you really want to know on the server side whether or not the browser client supports Javascript, you could try using cookies. On every HTML page, have a Javascript which checks for a 'i_can_script' cookie; if it's not set, try to set it. If it does set correctly, refresh the page. On the server side, you can then look for this cookie whenever you're spitting out a page; if it's there, the browser definitely does allow Javascript; if it doesn't, then the browser doesn't support Javascript, or doesn't permit Javascript, or didn't accept your cookie.

    a
Re: Verifying Javascript is enabled
by vagnerr (Prior) on Jan 23, 2002 at 21:27 UTC
    I don't think there is realy a way to do this. The browser does not pass that information to the server when it makes a request. You're best bet would probably be to put some javascript into the preceeding page that links to you're CGI script in order to add an extra form element or value. Something like
    onClick = document.forms['myform'].elements['isjavascript'].value = 1

    inside you HREF tag or if you waht to be realy simple just have something like
    <noscript>This site requires JavaScript to be enabled</noscript>


    ---If it doesn't fit use a bigger hammer
Re: Verifying Javascript is enabled
by Spudnuts (Pilgrim) on Jan 23, 2002 at 23:28 UTC
    This doesn't have much to do with perl, but you could have the basic page say that JavaScript is required to use it. Then, have a JavaScript function that is set to run on page load, and have it disable the non-JavaScript text and then show the proper content.
(cLive ;-) Re: Verifying Javascript is enabled
by cLive ;-) (Prior) on Jan 24, 2002 at 07:42 UTC
    Although the answer is no, you could do this...

    If you are calling the script from a form, include this:

    # in the form <form name="this_form" ...etc... > <input type="hidden" name="use_js" value=""> # elsewhere (after form, or call as onLoad in body tag) <script language="javascript"> document.this_form.use_js.value = "yes" </script>
    Then check for the value of the field use_js in your Perl script.

    A similar result can be achieved through query string info if it's a link instead.

    .02

    cLive ;-)