justjoe
justjoe

Reputation: 5564

How to check whether a JavaScript is active or not via PHP?

Is it possible to do checking and verification of JavaScript state in a browser ?

Upvotes: 2

Views: 1161

Answers (4)

eclipse31
eclipse31

Reputation: 396

At the top of your index.php you could do something like

<script type="text/javascript">
    window.location = '/index_with_js.php'
</script>

So, if the browser had JS enabled, it would be redirected to a special index page, which could in turn set a session variable, e.g. $_SESSION['has_js'] = true;

If the browser doesn't have JS enabled, the page won't be directed, so $_SESSION['has_js'] won't ever be set.

Upvotes: 1

c_harm
c_harm

Reputation:

That's the wrong way to do it. PHP should be outputting markup that works with JavaScript off, and you should be enhancing that markup with JavaScript. I suggest you read up on progressive enhancement.

Upvotes: 3

Justin Ethier
Justin Ethier

Reputation: 134197

Is there any reason you need to do this via PHP, instead of displaying a message in the browser using the NOSCRIPT tag?

Upvotes: 1

karim79
karim79

Reputation: 342665

No, not reliably. But it is possible to respond with something (on the client) if Javascript is not available using noscript:

<noscript>Your browser does not support JavaScript!</noscript>

Upvotes: 3

Related Questions