how to check if Javascript is enabled

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

how to check if Javascript is enabled

Post by jasongr »

Hello People

Is there a way to check in PHP whether the user browser supports Javascript?
and if so, how?

thanks
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

i dont think you can do that with PHP but you can just use the handy <noscript> html tag to process you non javascript users
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

no, a php is server side and javascript is clientside.
Sorry.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

I was also thinking if something is available to check whether Javascript is enabled or not it would be very useful.
For instance, I want Javascript validation for stopping unnecessary submission to server side script and for performance; if Javascript is not enabled then I can use my server side validation else I would just let the form fields to pass through without validation.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

You can check via php, but it requires a two-step process.

Have an html form. Make a js onsubmit function that sets a variable in the form ($has_js) to true.

If its not set after receiving the submit, they do not have js enabled.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Roja wrote:You can check via php, but it requires a two-step process.

Have an html form. Make a js onsubmit function that sets a variable in the form ($has_js) to true.

If its not set after receiving the submit, they do not have js enabled.
that is a good idea Roja... :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

raghavan20 wrote:I was also thinking if something is available to check whether Javascript is enabled or not it would be very useful.
For instance, I want Javascript validation for stopping unnecessary submission to server side script and for performance; if Javascript is not enabled then I can use my server side validation else I would just let the form fields to pass through without validation.
You should always validate server-side, the loophole in that logic is if an attacker writes up their own form to show javascript is enabled, but does not validate the input you could have some SQL injection or worse.

It is useful for reducing the number of failed validation requests, and that is about it :)
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Jenk wrote:
raghavan20 wrote:I was also thinking if something is available to check whether Javascript is enabled or not it would be very useful.
For instance, I want Javascript validation for stopping unnecessary submission to server side script and for performance; if Javascript is not enabled then I can use my server side validation else I would just let the form fields to pass through without validation.
You should always validate server-side, the loophole in that logic is if an attacker writes up their own form to show javascript is enabled, but does not validate the input you could have some SQL injection or worse.

It is useful for reducing the number of failed validation requests, and that is about it :)
Actually, there is another use for it.

As odd as it seems to pair JS with Usability, using JS properly, you can provide very useful feedback to the user on what they are doing wrong when filling out a form. From highlighting fields, to verifying that they've used the correct format, JS can be very useful in form completion.

But I agree about the validation - that should be done server-side, or else you are trusting the user for your security. Never a good idea.
Post Reply