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.

Community
  • 1
  • 1
Rakesh
  • 2,730
  • 10
  • 39
  • 65

6 Answers6

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

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
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

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>
Community
  • 1
  • 1
Anirudha
  • 32,393
  • 7
  • 68
  • 89
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

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.

Weacked
  • 954
  • 2
  • 7
  • 18
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

coolxeo
  • 553
  • 1
  • 4
  • 10