Organizational Research By

Surprising Reserch Topic

how to check if JavaScript is enabled with PHP and javascript


Is there a way to check if JavaScript is enabled with PHP / javascript on client side ? If so, how?

asked Jun 30, 2015 in JAVASCRIPT by rajesh
0 votes
49 views



Related Hot Questions

4 Answers

0 votes
No, that is not possible, because PHP is a server side language, it does not access the client's browser in any way or form (the client requests from the PHP server).

The client may provide some meta info through HTTP headers, but they don't necessarily tell you whether the user has JavaScript enabled or not and you can't rely on them anyway,
answered Jun 30, 2015 by rajesh
0 votes

perhaps a more simple option...




my content
answered Jun 30, 2015 by rajesh
0 votes

//Here is a solution: //it works perfect

php

if(!isset($_SESSION['js'])||$_SESSION['js']==""){
  echo "";
   $js = true;

 }elseif(isset($_SESSION['js'])&& $_SESSION['js']=="0"){
   $js = false;
   $_SESSION['js']="";

 }elseif(isset($_SESSION['js'])&& $_SESSION['js']=="1"){
   $js = true;
   $_SESSION['js']="";
}

if ($js) {
    echo 'Javascript is enabled';
 } else {
    echo 'Javascript is disabled';
}

?>

//And then inside get-javascript-status.php :

$_SESSION['js'] = isset($_GET['js'])&&$_GET['js']=="0" ? "0":"1";
header('location: /');
answered Jun 30, 2015 by rajesh
0 votes

You can try with 2 metod:

  • setting cookies with JS and detecting them from PHP
  • 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.

if you want detect if JS enable enable setting before the loading of the page you can try this (I don't konw if it works):

php
if (isset($_POST['jstest'])) {
  $nojs = FALSE;
  } else {
  // create a hidden form and submit it with javascript
  echo '';
  echo '';
  echo '';
  echo '';
  // 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
}
?>

there is a fine tutorial on this issue on address http://www.inspirationbit.com/php-js-detection-of-javascript-browser-settings/

answered Jun 30, 2015 by rajesh

...