Showing posts with label enabled. Show all posts
Showing posts with label enabled. Show all posts

Tuesday, August 3, 2010

PHP Check if Javascipt is Enabled in Browser

Do you want to check in php if javascript is enabled or not? Now, here is a simple trick in php to check if you have javascript enabled in your browser. The trick is that you have a form with a hidden value which will be set as on if it is submitted by a javascript code. Of course, if the form was not submitted, javascript is disabled in your browser. Please read the comments carefully so that you will be guided with the code.


<?php

// this would not work if no html tags
// you can comment the line after this if you already have a open html and body tag
echo '<html><body>';
// stores the value of js if the form is submitted otherwise off
$js = array_key_exists('js', $_POST) ? $_POST['js'] : '';
// if $js is on, the form was submitted by the javascript submit()
if ($js=="on"){
    echo "Javascript is enabled.";
}
// otherwise display the form and try to submit the form with javascript
else{
    echo '<form name="js_form" method="post"><input type="hidden" name="js" value="on"></form>';
    echo '<script type="text/javascript">document.js_form.submit()</script>';
    echo "Javascript is disabled.";
}
// you can comment the line after this if you already have a close body and html tag
echo '</body></html>';

?>