Page 1 of 1
Detecting Javascript
Posted: Sun Jul 24, 2005 12:17 pm
by programmermatt
I was wondering what would be the best way to auto-detect javascript ability. I was thinking of something like this:
Code: Select all
<html>
<head>
<meta http-equiv="e;refresh"e; content="e;2;url=index.php?js=0"e;>
</head>
<body>
<script type="e;text/javascript"e;>
document.location.href = "e;index.php?js=1"e;;
</script>
</body>
</html>
Posted: Sun Jul 24, 2005 12:36 pm
by hawleyjr
Posted: Sun Jul 24, 2005 1:06 pm
by programmermatt
Because I want to know on the PHP side wether they have javascript so I can conditionally output javascript to them or not and they don't have to worry about selecting wether or not they have javascript in their profiles and also allows guests to use the javascript features without logging in.
Posted: Sun Jul 24, 2005 2:43 pm
by programmermatt
I came up with this and was wondering if anyone sees potential problems with my javascript detection:
Code: Select all
// this _short_ section of code handles javascript detection
// js= 0 (no js) 1 (js) 2 (js+ajax)
if( !isset($_SESSION['javascript']) ) {
if( !isset($_GET['js']) ) {
ob_clean(); echo "<html><head><meta http-equiv=\"refresh\" content=\"1;url=index.php?js=0\">
</head><body>Detecting Javascript..<script type=\"text/javascript\">
try{ request = new XMLHttpRequest(); }catch(e){
try{ request = new ActiveXObject(\"Microsoft.XMLHTTP\");
}catch(e) { document.location.href = \"index.php?js=1\"; }
}; document.location.href = \"index.php?js=2\";</script></body></html>";
ob_flush();exit;
} else $_SESSION['javascript'] = $_GET['js'];
}
Posted: Sun Jul 24, 2005 10:37 pm
by Roja
programmermatt wrote:Because I want to know on the PHP side wether they have javascript so I can conditionally output javascript to them or not and they don't have to worry about selecting wether or not they have javascript in their profiles and also allows guests to use the javascript features without logging in.
You don't need to.
If they don't support javascript, their browser will not download the javascript (unless it is inline, and it shouldnt be - it should be a standalone file, so it can be cached!). This is true on IE, Firefox, Konqueror, AND Opera.
Instead, as suggested, you simply put the fallback functionality in a <noscript> tag.
As to detecting it, I always suggest
http://phpsniff.sourceforge.net