views:

214

answers:

5

I am looking for some kind of trick to determine if a user has javascript... I figure I could do it somehow by sending a ajax request at the top of a page, and in that ajax request, set a session variable, and then somehow reload the page, and see of it was set...

Is there any tricks like this around in PHP/AJAX?

A: 

you could use a very simple tip:

<script>document.location="/index2.php";</script>
<p>You need javascript for an improved navigation!</p>

where the current page (which I assume is index.php) is only used to redirect the visitor to the actual index page : index2.php

or use get_browser which provides a field for javascript. I tested it with firefox (disabling js and not) and it worked pretty fine for me.

Aif
you should set `window.location.href` instead of `document.location`
Christoph
A: 
$results = get_browser();
if ($results["javascript"] == 1) {
echo "You have javascript";
}

(source)

kkaploon
I believe that only tells you whether the user's browser is *capable* of Javascript, not whether it's actually been enabled or disabled.
cletus
I don't agree, see my post.
Aif
+2  A: 

You can always use the noscript tags:

<noscript>
<input type="hidden" name="has_javascript" value="false">
</noscript>

and detect that on the server.

cletus
+7  A: 

What you can do is add code like this (no AJAX needed):

<meta http-equiv="refresh" content="2;URL=/?nojs" />
<script type="text/javascript">
window.location.href="/?js";
</script>

However, you should not do that. Instead, serve the basic website every time, and decorate it with a script element that loads all the JavaScript that modifies and adds dynamic content.

phihag
Damnit. I was just about to post the same thing. :)
Emil H
A: 

aif is mistaken. Even if JS is disabled, if your browser supports JS it is listed as javascript=1.

meeech