Page 1 of 1

how to check if Javascript is enabled

Posted: Thu Feb 09, 2006 6:50 am
by jasongr
Hello People

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

thanks

Posted: Thu Feb 09, 2006 6:52 am
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

Posted: Thu Feb 09, 2006 6:52 am
by phpScott
no, a php is server side and javascript is clientside.
Sorry.

Posted: Thu Feb 09, 2006 7:38 am
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.

Posted: Thu Feb 09, 2006 8:04 am
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.

Posted: Thu Feb 09, 2006 9:33 am
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... :)

Posted: Thu Feb 09, 2006 9:44 am
by feyd

Posted: Thu Feb 09, 2006 9:58 am
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 :)

Posted: Thu Feb 09, 2006 11:00 am
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.