Online Shopping : 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?

46 of 72 people (64%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How can I check if the user has Javascript enabled in their browser?
Can I find out in PHP if the user has Javascript turned on?

Jun 18th, 2002 05:05
Pekka Saarinen, Nathan Wallace, Teodor Cimpoesu


You can check if the user has Javascript from PHP only if you get them
to send you a page.  This is because PHP runs on the server.
Here is a PHP script to check it:
    <html><body>
    <!-- other stuff -->
    <form name="tester" action="<%=$PHP_SELF%>" method="post">
    <input type="hidden" name="JS_SWITCH" value="JS_OFF">
    <!-- any other fields -->
    <input type="submit" value="click to continue!...">
    </form>
    <script><!--
        document.tester.JS_SWITCH.value="JS_ON";
    //-->
    </script>
    </body></html>
If JS works, it will turn on the switch, else you'll get the same
JS_OFF.
-------------------
(Pekka saarinen)
There is a good mechanism inbuilt to HTML, namely <noscript>. 
This lets you offer e.g. a form without submit button for those with 
javascript, and a form with submit button for those without javascript 
all in very simple code:
<FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD="POST" 
name="FORM_select_number" id="FORM_select_number">
<select name="select_number" onchange="function_submit_select_number
()">
<option value="1">1</option>
<option value="2">2</option>
<option value="2">3</option>
</select>
<script language="JavaScript" type="text/javascript">
<!--
function function_submit_select_number(){
document.FORM_select_number.submit();
	}
//-->
</script>
<noscript>
<input type="submit" value="do" name="SUBMIT_select_number">
</noscript>
</FORM>