View Full Version : detecting if JS is turned off
dirtwalker
10-07-2002, 08:08 PM
Hi all,
This is a general question. I use JavaScript to validate some input on a form. I have an onsubmit on the submit button that validates the entries once the user submits the form, but if they have JavaScript turned off, the submit button does not work at all....
I want the JavaScript checking done whenever possible, but not on users that elect to turn JavaScript off.................
So, can JavaScript test it if the browser supports JavaScript or if it is turned on or off, and if it does, would this even help me in this situation....
Thanks,
DW
beetle
10-07-2002, 08:52 PM
Think a moment about what you are asking....
"Can javascript detect if javascript has been turned off?"
That's like asking
"Can my computer detect if my computer is turned on?"
So, basically, what you are looking for is an existing characterstic of the system. Your validation routine just plain won't run on browsers with javascript turned off.
You can detect JS versions, but it's a moot point for the most part.
Roy Sinclair
10-07-2002, 09:54 PM
You still need server side checking for people who have javascript disabled but in order to allow those users to submit the form you need to have a real submit button. It sounds like you've only got a generic button right now. There are simple ways of making the submit button work for people with javascript disabled or who have their input verified by your script and also have it not submit for those who have javascript enabled and their are input errors which can be fixed without making the round-trip to the server.
Is that what you're trying to accomplish?
dirtwalker
10-08-2002, 12:18 AM
Thanks guys,
I see where you are coming from beetle, lol. And thanks Roy for the suggestion. I will now go this route. This is what I figured needed to be done, but was not sure and wanted to make sure I was headed in the right direction. I appreciate it.
DW
beetle
10-08-2002, 01:21 AM
What server-side languages do you have available to you, dirtwalker? I'm writing a PHP validation class that may help you...if you have PHP :D
whammy
10-08-2002, 01:26 AM
I have an extremely easy solution to use if you're using ASP (or another server-side scripting language) to figure out whether or not javascript is enabled... (and it works)... just document.write a hidden form field... i.e.:
<script type="text/javascript">
<!--
document.write('<input type="hidden" name="javascriptenabled" value="1" />')
// -->
</script>
If you request that field once the data has been submitted to the server, then you know right away if javascript is enabled.
Of course I'd always validate data in ASP (or PHP or whatever) anyway, but that's beside the point.
joh6nn
10-08-2002, 02:57 AM
why not
<form ... >
...
<noscript>
<input type="hidden" name="noscript" value="true">
</noscript>
</form>
?
whammy
10-08-2002, 03:04 AM
I've never considered nor tested that... however I know if javascript is enabled that document.write works. If I don't receive the value from the hidden field, then javascript isn't enabled in the browser... it seems fairly straightforward to me. :)
Althought it seems like the same end result would occur in that situation, but in reverse?
I'd have to play with it, since I don't know if <noscript> is still supported, and I'm not sure what the browser support is for that tag! (You know how hard it is to keep up with standards these days - which is why I propose everyone script in XHTML 1.1, and use stylesheets!).
joh6nn
10-08-2002, 03:55 AM
well, <noscript> is supposed to be universal (everything since NS2), but i just thought of a reason not to use it; the absence of javascript isn't always because the browser doesn't support it. sometimes it's because a company firewall strips it out. which means that you'll get better results using document.write() to test for the existence of javascript, than you will using <noscript> to test for the non-existence of javascript.
whammy
10-08-2002, 04:08 AM
I just figured that if you were able to document.write() a value to a form, and that value was retrieved on the server-side, then obviously javascript was enabled on the client-side. I try to keep things simple that way. :D
Although the situations you described are interesting things to consider...
realisis
10-08-2002, 05:30 AM
"the absence of javascript isn't always because the browser doesn't support it. sometimes it's because a company firewall strips it out. "
Er, in which case, you would still want the <NOSCRIPT> content to display, and the browser does just that.
Browsers which *do* support javascript, but which have javascript disabled, will run any content embedded within <noscript></noscript>... that's also part of the idea behind the tag.
joh6nn
10-08-2002, 05:40 AM
yeah, but what if you have a browser with javascript enabled, and a firewall or proxy server that removes the javascript from pages? the <noscript> tags won't be parsed, because the browser supports javascript, and has it enabled, but the javascript doesn't execute, because the firewall removed it. see what i mean now?
realisis
10-08-2002, 05:51 AM
sorry joh6nn, I misread you slightly.
"the absence of javascript" - I took this to mean a browser which doesn't recognize javascript (the language), rather than a webpage devoid of any javascript (statements). I was thinking more of administrators who might disable javascript across all browsers in a network (as you can with Opera, I think).
I also was not aware that firewalls could (or do) modify page-contents or strip things out...
Anyway, your rejoinder to me is correct: the browser in this case would not see the <NOSCRIPT> contents. My bad - I see your point now of course.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.