This question gets asked tons of times on forums I am a member of. How can I tell if a user has Javascript enabled in their browser using PHP (or ASP, Python, Perl, ROR, Java for that matter of fact).
Unfortunately, there is no way you can do that. The closest you can come to Javascript with a scripting language is to know if the browser is capable of handling Javascript, which can be done using the get_browser function in PHP:
<?php echo $_SERVER['HTTP_USER_AGENT'] . "\n\n"; $browser = get_browser(null, true); print_r($browser);
Which will output:
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3 Array ( [browser_name_regex] => ^mozilla/5\.0 (windows; .; windows nt 5\.1; .*rv:.*) gecko/.* firefox/0\.9.*$ [browser_name_pattern] => Mozilla/5.0 (Windows; ?; Windows NT 5.1; *rv:*) Gecko/* Firefox/0.9* [parent] => Firefox 0.9 [platform] => WinXP [browser] => Firefox [version] => 0.9 [majorver] => 0 [minorver] => 9 [cssversion] => 2 [frames] => 1 [iframes] => 1 [tables] => 1 [cookies] => 1 [backgroundsounds] => [vbscript] => [javascript] => 1 [javaapplets] => 1 [activexcontrols] => [cdf] => [aol] => [beta] => 1 [win16] => [crawler] => [stripper] => [wap] => [netclr] => )
My answer to that person was to use an intermediate index.html page, which will redirect them to the main page telling them if they have Javascript enabled or not:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Loading site ...</title> <meta HTTP-EQUIV="refresh" CONTENT="1;URL=index.php?js=no"> <script language="JavaScript"> <!-- window.location = "index.php?js=yes" // --> </script> </head> <body> </body> </html>
The way this works is that if the user has Javascript enabled, the window.location (or any other JS method you use) will be executed. If Javascript is disabled, then the Meta Refresh tag will kick in after 1 second. You probably can do the refresh at 0 second, but 1 second should give enough time for a browser that is using lots of memory (Sorry FireFox).