Check If JavaScript Is Enabled With PHP

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 check if Javascript is enabled or not in browser using PHP?

Try using ajax from client, if it works then javascript is enabled

With jQuery you can simply:

$.ajax({
url: "/checkjavascript.php"
});

if your php receives call from client then Javascript is enabled in the client

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 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 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>

Check whether Javascript is enabled

<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.

PHP & noscript combination to detect enabled JavaScript in browser

<noscript> tags

You can use the noscript tags to display content to browsers with javascript disabled or redirect them to another page (a nojs-version.php for example).

<!-- Redirect to another page (for no-js support) (place it in your <head>) -->
<noscript><meta http-equiv="refresh" content="0;url=nojs-version.php"></noscript>

<!-- Show a message -->
<noscript>You don't have javascript enabled! Please download Google Chrome!</noscript>

Modernizr

The better way to handle javascript detection (& feature) would be to use Modernizr: http://modernizr.com

Check out this SO question: What is the purpose of the HTML "no-js" class?

A basic example (without Modernizr)

You could add the class no-js on page load to your <body> tag. Then when the page loads and if javascript is enabled, you can replace the no-js with js like so:

// When the DOM is ready & loaded, do this..
$(document).ready(function(){
// Remove the `no-js` and add the `js` (because JS is enabled (we're using it!)
$('body').removeClass('no-js').addClass('js');

// Assign it to a var so you don't traverse the DOM unnecessarily.
var useJS = $('body').hasClass('js');
if(useJS){
// JS Enabled
}
});

The above code is a very basic example of how modernizr works. I would highly recommend just using that.

Check out Modernizr



Related Topics



Leave a reply



Submit