Test Javascript On/Off in PHP via POST


Javascript is almost enabled in modern browsers. However, at some legacy devices or some particular situations where the Javascript is not supported or turned on (for security purposes), you might want to do some checks.

The following is a good way to check if Javascript is turned on and pass the result into dynamic scripting language e.g. PHP although it makes no difference in other programming languages as well such as ASP or JSP.

1
2
3
4
5
6
7
8
9
<form action="" method="post"> 
<input type="hidden" name="JS" value="JS_OFF"> 
<!-- any other fields -->
<input type="submit" value="Submit">
</form> 
<script><!-- 
document.getElementById('JS').value="JS_ON"; 
//-->
</script>
<form action="" method="post"> 
<input type="hidden" name="JS" value="JS_OFF"> 
<!-- any other fields -->
<input type="submit" value="Submit">
</form> 
<script><!-- 
document.getElementById('JS').value="JS_ON"; 
//-->
</script>

So the default HTML sets the form element JS to JS_OFF and if the javascript is working, it will sets the field to JS_ON which then can be passed to the PHP (or ASP, JSP) as a hidden form variable.

No other complicated technique, simply HTML can PHP can do this.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
232 words
Last Post: BASH Shell Script to Compute the Integer Factorial
Next Post: C/C++ Function to Compute the Combination Number

The Permanent URL is: Test Javascript On/Off in PHP via POST

Leave a Reply