5

Possible Duplicate:
How to detect if JavaScript is disabled?

In my php application i need to check whether the javascript is turn on or not in browser. I have tried this <noscript><p>javascript is off<p></noscript> it's working fine.But i need to redirect to a page if javascript is OFF so developed like this

<noscript>
<?php header('Location: index.php');?>
</noscript>

But i/ts always redirecting to index.php, there is any way to do this.

0

6 Answers 6

11

your solution clearly cannot work, since php is executed before the page is served on client

you could instead do something like

<noscript>
  <meta http-equiv="refresh" content="0;URL='http://example.com/'">
</noscript>

in the head of your document

Sign up to request clarification or add additional context in comments.

Comments

3
<noscript><meta http-equiv="refresh" content="1;url=error.html"></noscript>

try this to redirect if JS is disabled Php script runs independently of js

Comments

2

There are known issues with noscript

Instead do this:

<html>

<head>

<script>
window.location.href="javascriptEnabled.html";
/*Since javascript is enabled redirect to javascript based page*/
</script>

</head>

<body>
Showing Static Website since javascript is not enabled..
</body>

</html>

Comments

1

There is not a good way to do it, because without client side interactivity, you can not get information back from the client.

You could do something like this...

  1. Set up a meta redirect to trigger after 5 seconds
  2. use javascript to override the meta redirect

Therefore, if javasccript is enabled, you get to the javascript page, otherwise the non javascript page

Comments

0

It does redirect to index.php because the <?php header('Location: index.php');?> is processed by the server while the <noscript> is processed by the client ..

A correct way to do this is to set a cookie using javascript on the client, then check for the cookie's presence with php on the server, If the cookie does exist, then javascript is on, Else, it's off.

Comments

0

You could use a different approach here

  • Use the no javascript version as default version
  • Use a javascript redirect to the enhanced version

Remember it's better to not have separate versions of the site. You could progressive enhancement with libraries as http://modernizr.com/ or https://github.com/filamentgroup/enhance

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.