How to Detect If JavaScript Is Disabled

How to detect if JavaScript is disabled?

I assume you're trying to decide whether or not to deliver JavaScript-enhanced content. The best implementations degrade cleanly, so that the site will still operate without JavaScript. I also assume that you mean server-side detection, rather than using the <noscript> element for an unexplained reason.

There is no good way to perform server-side JavaScript detection. As an alternative it is possible to set a cookie using JavaScript, and then test for that cookie using server-side scripting upon subsequent page views. However this would be unsuitable for deciding what content to deliver, as it would not distinguish visitors without the cookie from new visitors or from visitors who are did not accept the JavaScript set cookie.

How to check if javascript is enabled?

Use a noscript tag:

<noscript>Javascript required</noscript>

How to inform if Javascript is disabled in the browser

If informing your users to enable it is all you want to do, you don't need to depend on server-side code (in fact, I don't think you can). Just use a <noscript> element:

<noscript><p>Please enable JavaScript in your browser for better use of the website.</p></noscript>

Detect if JavaScript is disabled

You could try putting an image inside the <noscript> tag, which would point to a php file of yours, which in turn it should return an image. This could allow you to know in the server that the user has Javascript disabled.

How to identify the user: you could rely on the session, or set an ID to the url of the image.

You could use the answer on this question as an example on how to server image files from a php script where you could add your logic to detect if the user has js disabled:

Return a PHP page as an image

How to detect if a user had javascript disabled?

Don't try to build separate JS and non-JS versions of the site. Build a non-JS version and then enhance it with JS. This makes it easier to reuse code, allows you to use object/feature detection for the entire stack, and makes it less likely that users without JavaScript will be left behind if developers update one branch of the site but not the other.

Check if JavaScript is enabled with PHP

No, that is not possible, because PHP is a server side language, it does not access the client's browser in any way or form (the client requests from the PHP server).

The client may provide some meta info through HTTP headers, but they don't necessarily tell you whether the user has JavaScript enabled or not and you can't rely on them anyway,

How to detect that JavaScript and/or Cookies are disabled?

For checking cookies you can use:

function checkCookie(){
var cookieEnabled = navigator.cookieEnabled;
if (!cookieEnabled){
document.cookie = "testcookie";
cookieEnabled = document.cookie.indexOf("testcookie")!=-1;
}
return cookieEnabled || showCookieFail();
}

function showCookieFail(){
// do something here
}


// within a window load,dom ready or something like that place your:
checkCookie();

And for checking JavaScript use a <noscript> tag with some kind of message inside



Related Topics



Leave a reply



Submit