faqts : Computers : Programming : Languages : PHP : Not Quite PHP : Javascript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

109 of 144 people (76%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How can I check if the user has Javascript enabled in their browser?

Jan 25th, 2006 12:20
David Stächele, Ruedi Birenheide, Onno Benschop, http://www.faqts.com/knowledge-base/view.phtml/aid/464/fid/47


Based on the script of Marcel Hicking, I wrote a short script that does
not have a parameter added to the URL. I wanted to have a script that
lets me count how many users have javascript enabled that visit a
certain page, and if the parameter is automatically added to the URL, it
would be in the bookmark as well. Any user coming from a bookmark with
'js=on' added to the URL would be assumed to have javascript on, even if
he has switched it off.
I wrote a script that uses the ability of javascript to submit forms, in
this case back to the same page. If javascript is on, the form is
submitted and the parameter js is set to "on". If javascript is off,
nothing will be submitted and the parameter js stays on "off". This
works without parameters added to the bookmark, and still runs
automatically. 

Have fun!
Ruedi



<HTML>
<BODY>

<?php
$js = array_key_exists('js', $_POST) ? $_POST['js'] : '';
if ($js == '')
{
   $js = 'off';
?>
    <form name="tester" action="<?php echo $PHP_SELF ?>" method="post">
    <input type="hidden" name="js" value="on">
    </form>
    <script type="text/javascript"><!--
	document.tester.submit()
    //-->
    </script>
<?php 
    }
?>
Javascript is <? echo htmlentities($js); ?>
<BR>
here follows main body.    
</BODY>
</HTML>