Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Testing for JavaScript

by akm2 (Scribe)
on Mar 13, 2001 at 20:32 UTC ( #64137=sourcecode: print w/replies, xml ) Need Help??
Category: CGI Programming
Author/Contact Info Andrew Kenton Mitchell
Description: I had a situation where I needed to know if my visitor's browers supported JavaScript. Here is the code I used.

#Hash Array containg list of pages to display depending on test result
%resultURLs = (
                'PASS', '',
                'FAIL', '
#URL of this script.
$scriptURL = "/cgi-bin/";

#Read hidden form results and decide what to do.
require "lib/";  #You can use if you like.
if (!$in{'jstest'}){
$result = uc $in{'jstest'};
$url = $resultURLs{$result};
print "Location: $url\n\n";
#Print HTML output that handles the testing.
sub runJSTest{
print <<END_OF_HTML;
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1
#Redirect to error page if JavaScript found within 5 seconds.
<meta http-equiv="refresh" content="5;URL=$scriptURL?jstest=fail">

<body bgcolor="#FFFFFF" onload="testJS()">
<script language="JavaScript">
#If JavaScript is enabled this functions is run thereby returning a va
+lue of true.
function testJS() {

<table width="7%" border="0" cellspacing="0" cellpadding="0" align="ce
    <td nowrap align="center"> 
      <p><b><font face="Arial">PLEASE&nbsp;WAIT<b><img src="/images/pe
+riod_ani.gif" width="18" height="12"></b></font></b></p>
      <p><font face="Arial"><b>CHECKING&nbsp;YOUR&nbsp;SYSTEM&nbsp;FOR
+&nbsp;INSTALLED&nbsp;COMPONENTS<img src="/images/period_ani.gif" widt
+h="18" height="12"></b></font></p>
      <p><b><tt><font face="Arial">IF&nbsp;THIS PAGE&nbsp;DOES&nbsp;NO
+T&nbsp;CHANGE&nbsp;WITHIN&nbsp;10&nbsp;SECONDS,&nbsp;PLEASE&nbsp;<a h
#Hidden form that returns the results, if JavaScript is enabled.
<form name="jstestform" method="post" action="$scriptURL">
  <input type="hidden" name="jstest" value="pass">

2001-03-13 Edit by Corion : Fixed formatting

2001-03-13 Edit by akm2 : Enhanced code readibility

Replies are listed 'Best First'.
Re: Testing for JavaScript
by arturo (Vicar) on Mar 13, 2001 at 20:42 UTC

    A few comments here that might help you improve the code, maintainability-wise.

    I'd use CGI for parsing the input, even if you don't like the code generation functions it provides (you appear to be working with some old docs, or is it an old version of Perl?)

    # to duplicate and ReadParse (not that you need to -- you c +ould use more native methods) use CGI qw(param); my %in = map {$_ => param($_) } param;

    Instead of that gaggle of print statements, you might check out using "here documents", which work like:

    print <<END_OF_HTML; <html> <head> <title>Hi!</title> </head> <body> <h1>Javascript is $in{jstest}</h1> <p>Isn't that neat? </body> </html> END_OF_HTML

    I find that sort of thing easier to maintain and less visually messy (no escaping quotes!)

    Oh, and I *strongly* recommend use strict (and, for that matter). Also, since this is a CGI, you should use the -T switch on your #! line.

    I like your use of the JS form submission, that's a good way around the problem with the cookie solution I suggested!

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://64137]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2017-03-28 03:06 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (326 votes). Check out past polls.