Check if javascript is available and redirect if javascript is not available

Possible duplicate:
How to determine if JavaScript is disabled?

In my php application, I need to check if javascript is enabled or not in the browser. I tried this <noscript><p>javascript is off<p></noscript> , it works fine. But I need to redirect to the page if javascript is off as advanced as this

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

But i / ts is always redirected to index.php, there is a way to do this.

+4
source share
6 answers

your solution obviously cannot work, since php is executed before the page is sent to the client

you could do something like

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

in the title of the document

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

try redirecting if js is disabled php script works regardless of js

+3
source

Known issues with noscript

Instead, do the following:

 <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> 
+2
source

There is no better way to do this, because without interactivity on the client side, you cannot receive information from the client.

You can do something like this ...

  • Configure meta redirect to trigger after 5 seconds
  • use javascript to override meta redirects

Therefore, if javasccript is enabled, you get to a javascript page, otherwise a page without javascript

+1
source

It redirects to index.php because <?php header('Location: index.php');?> processed by the server, and <noscript> processed by the client.

The proper way to do this is to set a cookie using javascript on the client, and then check for the presence of a cookie with php on the server. If cookie exists then javascript is on, Else, off.

0
source

Here you can use a different approach.

  • Use javascript version as default version
  • Use javascript redirection for advanced version

Remember that it is better not to have separate versions of the site. You can progress with libraries like http://modernizr.com/ or https://github.com/filamentgroup/enhance

0
source

Source: https://habr.com/ru/post/1436791/


All Articles