By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,944 Members | 1,503 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,944 IT Pros & Developers. It's quick & easy.

Using PHP to detect JavaScript support

P: n/a
I would like to use PHP (or any other server-side technique) to detect
if JavaScript was turned on in the user's browser. Is there any known
way to do this, and if not, why not!

I realise I could serve one page to both, and have the basic function
there by default and use DOM to replace it with another, but it would
be more efficient and lower overhead to serve different pages to
different UAs. Any suggestions welcome.

Now if I was Galactic Overlord, browsers that supported JavaScript
would be required to declare so in the Accept-Content HTTP header they
send.
Since I am not on usenet much, could anyone who replies pleace CC my
email address?

- Nick

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
*** Nicholas Shanks wrote/escribió (Tue, 26 Apr 2005 11:56:41 +0100):
I would like to use PHP (or any other server-side technique) to detect
if JavaScript was turned on in the user's browser. Is there any known
way to do this, and if not, why not!


Use an initial test page where JavaScript creates a variable and sends it
to server (hidden form field or URL attribute, cookie...) and read that
value afterwards from PHP. Just be aware that cookies can be disabled and
user may reach your page skipping the inicial test page.

However, I'd say its a thousand times easier to just write code that works
100% in server and use client-side JavaScript for UI enhancements. Why work
twice?
--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Jul 17 '05 #2

P: n/a
Nicholas Shanks <co*****@nickshanks.com> wrote:
I would like to use PHP (or any other server-side technique) to detect
if JavaScript was turned on in the user's browser. Is there any known
way to do this, and if not, why not!
No, because it is a clientside setting serverside environments don't
know until told (by the client).
I realise I could serve one page to both
How do you know what page to serve? That is what you are asking!
and have the basic function there by default and use DOM to replace it
with another
What DOM (if any)?
but it would be more efficient and lower overhead to serve different
pages to different UAs. Any suggestions welcome.


I beg to differ, serverside UA sniffing is almost always very poorly
implemented (take a look at the browsercap function and have a good
laugh). But than again clientside UA sniffing is also faulty in many
cases.

If you really want to server different pages to different available
capabilites there is an easy way to distinguis between js and nojs:
http://tmp.tryba.nl/hasjs/

Jul 17 '05 #3

P: n/a
You could use a button/link with a page in the href-part,
and an e.g. onClick="document.location.href='page2.php';return false"
or a document.write etc...

Jul 17 '05 #4

P: n/a
Alvaro G Vicario wrote:
*** Nicholas Shanks wrote/escribió (Tue, 26 Apr 2005 11:56:41 +0100):
I would like to use PHP (or any other server-side technique) to detect
if JavaScript was turned on in the user's browser. Is there any known
way to do this, and if not, why not!


Use an initial test page where JavaScript creates a variable and sends it

<snip>

Wouldn't it be simpler to just start each non-js page with js code to
redirect to the js-enabled version. Point the user towards the non-js entry
points and bingo.

C.
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.