Jump to content

is it possible to check if javascript is enabled using php?


Recommended Posts

php by itself can't determine if javascript is enabled.  The most it can do is know of it's supported by the browser, which doesn't help.  You will need to use javascript, or maybe some AJAX to see if it's enabled.  (as far as I know of course)

Beacuse it is client side scripting, it isn't possible. To do it in Javascript/HTML do this.

<script type="text/javascript">

document.write('Javascript is enabled');

</script>

<noscript>Javascript is disabled</noscript>

 

 

Or you could do this:

You have like the page you want to show on a get statement and redirect the user with javascript. If the user isn't being redirected, then JS isn't enabled else if the user is Javascript is enabled for them

<script type="text/javascript">

document.location.href = "page.php?js=enabled";

</script>

  • 1 month later...

Not tried this yet, but an idea...

 

On your PHP page, include the following:

 

<body onload="jsstatus()">

<form id="jstest" action="index.php" method="post">

 

<?php

 

echo '<input type="hidden" id="jstestinput"';

 

if (isset($_POST['jstestinput']))

{

echo ' value="'.$_POST['jstestinput'].'" ';

}

else

{

echo ' value="0"';

}

 

echo '/>

?>

 

</form>

.........

 

(amend the index.php to the name of the page in question)

 

In your .js file, include the following:

 

function jsstatus()

{

  if (document.getElementById('jstestinput').value == 0)

  {

  document.getElementById('jstestinput').value = 1;

  document.getElementById('jstest').submit();

  }

}

 

And back in the PHP file, add:

 

if (isset($_POST['jstestinput']))

{

$js = $_POST['jstestinput'];

}

else

{

$js = 0;

}

 

You can then use the variable $js within the page to specify actions to take if JS is enabled ($js = 1) or not ($js = 0) by using:

 

if (isset($js) && $js == 1))  //js enabled

{

...

}

else      //js disabled

{

....

}

 

 

When the page loads initially, if JS is not enabled, the value of the hidden field stays at 0 and the page is not submitted, hence $js does not get set.

If JS is enabled, the hidden field changes from 0 to 1, and the form is submitted. When the page reloads, the hidden field retains the value submitted (i.e. 1), so the JS function does not run again, and the $js variable is created.

 

  • 5 months later...

Hi

 

Can't think of a way to know in advance. However you could put a bit of javascript into the first page they visit and set a cookie with a short expiry time. Then php can just check for the existence of that cookie to know if Javascript is enabled. I have done similar things to get the screen resolution in php to resize images to fit.

 

You could also load a non Javascript login page and use Javascript to redirect to the full login page, but this would be messy.

 

All the best

 

Keith

  • 3 years later...

<?
if($_SESSION['JSexe']){		//3rd check js
if($_COOKIE['JS'])	setcookie('JS','JS',time()-1);//check on every page load
else			header('Location: js.html');
}				//2nd so far it's been server-side scripting. Client-side scripting must be executed once to set second cookie.
			//Without JSexe, user with cookies and js enabled would be sent straight to js.html on first page load.
elseif($_COOKIE['PHP'])		$_SESSION['JSexe'] = true;
else{				//1st check cookies
if($_GET['cookie'])	header('Location: cookies.html');
else{
			setcookie('PHP','PHP');
			header('Location: '.$_SERVER['REQUEST_URI'].'?cookie=1');
}
}
?>

<head>
<script type="text/javascript">document.cookie = 'JS=JS'</script>
</head>

 

I realize this thread is quite old but I wanted to share my script and also see if there is anyone out there with other solutions. It uses PHP, javascript and cookies, and it checks whether cookies and js are enabled or not.

 

It's quite simple really but if you need an explanation step by step you can find it here http://asdlog.com/Check_if_cookies_and_javascript_are_enabled

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.