I was looking for a way to detect whether a user has disabled JavaScript in a browser or not. I needed to execute some PHP code only when JavaScript is OFF.

There are several ways on detecting if JS is enabled or disabled in a browser, but most of them didn’t work for what I wanted to do:

  • with <noscript> tags – but my php code wasn’t regonized there
  • setting cookies with JS and detecting them from PHP – it was too long of a process for me, and I didn’t want to go with the cookies option
  • creating a form with a hidden field and an empty value; and then assigning some value to it with JS, if the field gets the value – JS is ON, otherwise it’s off. But the form had to be submitted first before PHP can request that hidden field’s value. For my case, I needed to detect JS right on the same page

Finally, after checking different options, I wrote this little script. Hopefully, others will find it useful too:

if (isset($_POST['jstest'])) {
  $nojs = FALSE;
  } else {
  // create a hidden form and submit it with javascript
  echo '<form name="jsform" id="jsform" method="post" style="display:none">';
  echo '<input name="jstest" type="text" value="true" />';
  echo '<script language="javascript">';
  echo 'document.jsform.submit();';
  echo '</script>';
  echo '</form>';
  // the variable below would be set only if the form wasn't submitted, hence JS is disabled
  $nojs = TRUE;
if ($nojs){
  //JS is OFF, do the PHP stuff

A hidden form is created here with PHP and is submitted with JavaScript as soon as the page loads. Thus if JS is enabled PHP gets hold of the form variable $_POST['jstest'], and the variable $nojs is set to be FALSE, otherwise $nojs is set to be TRUE. The last line will execute the PHP code only if JS is disabled, which meand that $nojs is TRUE.

Recent Bits
Related Bits
8 Bits From The Past
When Is It Wrong To Call A Typeface Font
16 bits of Exceptional Typography Resources
The Secret Truth About The Plugins Security
Magic Spell
Exporting / importing database tables
Time Machine, or 8 bits from the past
Image Manipulation Quick And Easy Way
Comment Bits

10 Insightful Bits in response to “PHP+JS Detection of JavaScript browser settings”

  1. Thanks for the info above, i was playing around with some ideas and came up with the below, it seems to work fine and doesnt require hidden form fields or re-directions like other methods i have found.


  2. staima

    @ derobonson:

    you script will always give $js=1:

    js: document.write([?php $js=1; ?]);

    php will always process that line and set js to on — even when js is off… :(‘

  3. Sid

    This script does not work with internet explorer.

  4. Vivien

    Hi Sid. Are you getting any errors in IE when you run the script?

  5. Stephen

    fyi… I initially posted a comment critical of your statement that your PHP code wasn’t recognized in the noscript tag… but then I realized that you might not mean what I thought you meant. I thought you meant that the tag would influence the parsing of your PHP, which of course is not true. However, it seems that what you mean is that whatever you output within noscript will not be recognized by browsers with Javascript turned on… which of course is true.

    It’s a good idea to use noscript when you can, though, because it’s designed for this very purpose. And in general, you can usually figure out ways to make a page downgrade gracefully without relying on more extreme solutions like automating a hidden form submission. To each their own, though, I guess :).

  6. I know this is an old post but i will leave you guys the option that I used and worked on FF and IE, the only that i found was (i dont know if its good or bad) that the page blinks a lil bit, check it out:

    ” name=”jscheckform” method=”post”>


    No JS”);

    i just included this file on every page and works fine to me, any comments will be apprecieated!


  7. Craig

    Thanks for this great bit of code. Very similar to how I was trying to achieve the same function, but much simpler.

    Haven’t actually tested your version yet, but this could also use a session variable so that the script doesn’t have to be run every time a page loads.

    Edit: the only downfall of using a session variable would be if a user turned javascript off after loading the page the first time. But of course there’s always a way around.

  8. You’re welcome, Craig. Nowadays I don’t think that any user would benefit from turning JavaScript off, but I guess there would still be such users.

  1. Anonymous

    How to detect JavaScript support with JS and PHP…

    A useful post on detecting whether JavaScript is set to OFF in a client’s browser or ON. This detection is done with JavaScript and PHP and very helpful for including in a PHP code….

Selected Bits





Hi, I'm Vivien. Thanks for visiting my Inspiration Bit. I often find myself scouring the internet looking for either answers to many questions I have or websites that inspire me, sites that I can learn from. On what topics you might ask — any topics that interest me, anything from web design to typography and art, from blogging to entrepreneurship, from programming to open source.
read more…
When I'm not blogging, I design web sites, teach, play with my daughter and try to balance family, work, friends and a somewhat active social life on