3

Can I let PHP know that a user does not have javascript enabled?

5
  • Not directly, what are you trying to do? Commented Aug 10, 2009 at 14:28
  • 1 idea is, I have notifications that show on a site using AJAX to include a php file, if JS is not active I would like to include a different file block wihtout any of the js or ajax in it Commented Aug 10, 2009 at 14:35
  • One idea is to have the page update using AJAX if JavaScript is on, otherwise when clicking the button/whatever makes your page show the message, the page will submit like it did in ye olde days. Example: <form onsubmit="ajaxHandler(this); return false;" action="handler.php" method="post"> -- the form will never submit if JavaScript is enabled. Note: You should apply the events separately from the HTML, for neatness' sake. Commented Aug 10, 2009 at 15:03
  • Problem is that is isnt a form, it loads page into a div every x amount of seconds Commented Aug 10, 2009 at 15:04
  • Send your HTML as if the user doesn't have JavaScript and add an onload event to the page that swaps out the JavaScript disabled content Commented Aug 10, 2009 at 20:30

4 Answers 4

2

Start by assuming javascript is off or not available, then send the client some HTML which includes this

<script>
window.location = "http://www.mysite.com/javascript.php?enabled=true";
</script>
<noscript>
possible tell the user the consequences of not having javascript enabled

</noscript>

In the javascript.php script, you can store in the session that the client supports javascript. You could also do with with an XMLHTTPRequest object rather than a straight redirect.

However, depending on your application, it's almost always better to build your app to work without Javascript, perhaps less efficiently, and then have your script kick in and augment the basic version with enhanced functionality. That way, you don't need to detect it, you just use it if its available.

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

4 Comments

While I agree that you should assume that JavaScript is off for most pages, I don't agree that you should redirect the user if they have JavaScript on. It's much better if the page naturally degrades if you don't have JavaScript on. There are many ways to do this, but they vary depending on what you want to do. But unless your page absolutely requires JavaScript, you should never redirect the user like in the above example, in my opinion (and if it does require JavaScript, there is no point in redirecting the user anyways.)
Being as my site is heavy on jquery and javascript, I think only allowing JS enabled users might happen soon, it's a social network, if they want to use the network I think requiring javascript is not bad, youtube requires you to have flash if you want to use there service
Yeah, we're moving towards a JavaScript-only web. Actually, if you turn JavaScript off, you'll find that YouTube is quite handicapped as well.
@rpflo: how does this "break" anything? If JS is disabled then the user sees that page; if JS is enabled then the user essentially sees a different page and the "non-js" page is removed from the history (which is what you want, right?)
2

You can have JavaScript let PHP know by setting a cookie once the user enters your page, for example. You won't know until the next request to the server, though.

You could also have JavaScript do a request with XMLHttpRequest that tells PHP to set a session variable. Still, you won't know until the next request.

What are you trying to do? There might be other solutions to your problem as well.

Comments

0

Top results on a google search:

http://www.inspirationbit.com/php-js-detection-of-javascript-browser-settings/

4 Comments

That'll kill the back button--and any developer that kills the back button ought to have his license revoked.
The author of that article makes some erroneous assumptions about the way the client/server interaction works with PHP. Sure, if output buffering is off the form might ideally be submitted immediately as the client receives the instruction, cutting the PHP script short (Connection Closed), but practically, the PHP script will have enough time to execute further before this happens and can't be relied on at all. Some very similar code could be written to do this properly, but it shouldn't rely on the PHP dying in mid-code.
@rpflo I didn't think about the back button being killed, good point. Maybe if I get some free time tonight, I'll take a closer look and "rewwrite" a proper solution :)
that is just "window.location = '?javascript=true';" but with an ugly hidden form and partial rendering of the page before submit.
0

Sure, just use an AJAX call to... ;)

I can't really think of a way to do this without direct user interaction. If you have some form/link that your user is going to submit to the server anyway, you could have a section of it (some <input element, or add an extra URL parameter to a link) within <noscript> tags such that that data only gets submitted by the user when Javascript is off.

2 Comments

if there is no javascript enabled, then how are they going to make an ajax call?
Well I read it as similar to one of the other answers - start by assuming JS is disabled, then use an AJAX call to update...

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.