Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether Javascript is enabled

Is there a way to check whether Javascript is enabled or supported by the browser? If it's not supported, I would like to redirect the user to a user-friendly error page.

I am using jQuery and the PHP Zend Framework.

like image 341
Jake Avatar asked Jul 13 '11 18:07

Jake


People also ask

How do I know if JavaScript is enabled in Windows 10?

Click the wrench icon on the toolbar. Click Options > Under the Hood. In the Privacy section, click Content settings. Scroll to the JavaScript section and click Allow all sites to run JavaScript (recommended).

How do you check if JavaScript has been disabled?

Verify that JavaScript is disabled. First, click on the "Security" tab. Under the "Web content" section, you'll see a box next to "Enable JavaScript." If the box is unchecked, that means JavaScript has been disabled.

Is JavaScript enabled in Chrome?

On Google Chrome, JavaScript is enabled by default, but you can verify if it works through the Settings menu. To reveal the Settings menu, simply click on three tiny black dots at the top-right corner of your Chrome window.


2 Answers

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

This will redirect to an error page if script is disabled. Just replace error.html with the URL of your error page.

like image 79
Mike Samuel Avatar answered Sep 30 '22 07:09

Mike Samuel


As yet another option, you can (though it requires a second page visit) use javascript to set a cookie.

If the cookie exists server-side (they have javascript) render the page as normal. During the absense of the cookie, you can either use a Location redirect, or render the appropriate [stripped-down] template to accommodate their lack of javascript support.

page html

<script type="text/javascript">
  document.cookie = 'hasJS=true';
</script>

page php

if (isset($_COOKIE['hasJS'])){
  // normal page render
}else{
  header('Location: http://mysite.com/index-nojs.php');
}
like image 42
Brad Christie Avatar answered Sep 30 '22 07:09

Brad Christie