3

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

JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • Not directly, what are you trying to do? – Ryan Florence Aug 10 '09 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 – JasonDavis Aug 10 '09 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: `
    ` -- the form will never submit if JavaScript is enabled. Note: You should apply the events separately from the HTML, for neatness' sake.
    – Blixt Aug 10 '09 at 15:03
  • Problem is that is isnt a form, it loads page into a div every x amount of seconds – JasonDavis Aug 10 '09 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 – rojoca Aug 10 '09 at 20:30

4 Answers4

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.

Blixt
  • 49,547
  • 13
  • 120
  • 153
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.

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • 3
    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.) – Blixt Aug 10 '09 at 15:00
  • 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 – JasonDavis Aug 10 '09 at 15:07
  • 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. – Blixt Aug 10 '09 at 16:13
  • @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?) – DisgruntledGoat Aug 10 '09 at 22:23
0

Top results on a google search:

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

Jefe
  • 598
  • 1
  • 7
  • 17
  • 3
    That'll kill the back button--and any developer that kills the back button ought to have his license revoked. – Ryan Florence Aug 10 '09 at 14:27
  • 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. – Blixt Aug 10 '09 at 14:32
  • @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 :) – Jefe Aug 10 '09 at 16:14
  • that is just "window.location = '?javascript=true';" but with an ugly hidden form and partial rendering of the page before submit. – bucabay Aug 13 '09 at 02:50
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.

Sarah Vessels
  • 30,930
  • 33
  • 155
  • 222