0

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?

3
  • what is your ultimate goal here? Do you just want to know on the client side (for CSS or something) or on the server side? Either way, this is a dupe... Commented Jun 23, 2009 at 23:14
  • A number of answers have attempted to actually answer this, but you are probably asking the wrong question. Build on stuff that works: icant.co.uk/articles/pragmatic-progressive-enhancement Commented Nov 30, 2009 at 15:48
  • possible duplicate of How to redirect if javaScript is disabled? Commented Sep 28, 2012 at 23:38

5 Answers 5

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Damnit. I was just about to post the same thing. :)
2

You can always use the noscript tags:

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

and detect that on the server.

Comments

0

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

Comments

-2

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.

1 Comment

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

(source)

1 Comment

I believe that only tells you whether the user's browser is capable of Javascript, not whether it's actually been enabled or disabled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.