Javascript, Web Development

PHP Detect Whether User has JavaScript (JS) enabled

Author Note: To the people who wanna seem elite or 1337:- JavaScript checks are real and do need to be done in certain situations so before you say that javascript checks are noob take a look in the mirror and you’ll see noob.

Edit: after I made this post I saw this http://scvinodkumar.wordpress.com/2009/11/17/determine-whether-javascript-is-enableddisabled-via-php/. I have had my own theory of making a AJAX poll if the user has javascript otherwise the user don’t.

Now I recently came over a design flaw when building a JQuery IM for my site. I relised (just like facebook) I need a way to stop those without javascript from appearing online to people with javascript. This is since those with javascript can use the IM and those without can not.

I had my own idea of surrounding a html form with PHP variables and using Javascript to submit that form. Whether or not this form subbmitted would tell if the user has JavaScript or not. I tried to find a better version or method from the Internet but this was the best I found (taken from bluejon):

<?php
ob_start();
session_start();
//Needs html tags to work !!
//Needs html tags to work !!
?>
<html>
<body>
<a  href="http://snippets.bluejon.co.uk/index.php"/>MENU</a>
<?php
$_SESSION['js']= array_key_exists('js', $_POST) ? $_POST['js'] : '';
if (empty($_SESSION['js'])){
  
  ?>
    <form name="postJs" action="<?php echo $PHP_SELF ?>" method="post">
    <input type="hidden" name="js" value="on">
    </form>
    <script type="text/javascript">
            <!--
          document.postJs.submit();
        //-->
    </script>
  <?php
}

if ($_SESSION['js']== "on"){
  print"<h3>Javascript is ON</h3><br>";
  print"Php code can be added here!<br>";
}else{
  print"<h3>Javascript is OFF</h3><br>";
  print"Php code can be added here!<br><br><br>";
}
ob_end_flush();
?>

<a  href="http://snippets.bluejon.co.uk/check4-js-and-cookies/check4-js-enabled-v2-phpcode.php">Display php source code </a>
</body>
</html>

However there are problems to this I am currently working on a better version that wont redirect the page twice.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s