How Can Forms Make Sure JavaScript Is Turned On?
The title is a popular question, one I've addressed on
several occasions. The responses and posts and articles are
scattered about on several of our domains.
Many forms use JavaScript to validate user input, prevent
double-clicking, prevent auto-submission, and dynamically
change field values. Among other things.
Without JavaScript, those forms don't work right.
The purpose of this article is to gather together the
various methods of ensuring browsers have JavaScript turned
on when a form is being used.
Omitting the Form
This method omits the entire form for non-JavaScript
browsers. It's just not there.
Deliver the form with JavaScript. JavaScript browsers will
see it. The others won't.
http://bontragercgi.com/Convert_Text_to_JavaScript.html
may be used to prepare the JavaScript. Once prepared,
paste the JavaScript in place of the HTML form code.
A NOSCRIPT tag can provide content in the form's stead for
non-JavaScript browsers.
Redirecting To a Different Page
This method sends non-JavaScript browsers to a different web
page.
JavaScript browsers will remain on the page. The others are
redirected.
Put something like this into the HEAD area of the web page
with the form:
<noscript>
<meta
http-equiv="refresh"
content="0; URL=http://example.com/page.html">
</noscript>
Replace the URL in the meta tag with the URL of the web page
where non-JavaScript browsers shall be redirected to.
Omitting the Submit Button
This method omits the submit button for non-JavaScript
browsers. It's just not there.
Deliver the submit button with JavaScript. JavaScript
browsers will see it. The others won't.
Here is an example:
document.write('<input type="submit" value="Send Form">');
Put the JavaScript between SCRIPT tags and use it instead of
the HTML submit button code.
It may be kind to use a NOSCRIPT tag to let users of
non-JavaScript browsers know to turn JavaScript on.
Choosing
The three methods are each quite different from the others.
The first omits the entire form. The second redirects to a
different web page. And the third omits only the submit
button.
Choose the one most useful in your situation.
In case you've wondered, a method that does not work so well
is disabling the submit button.
With that method, the button is disabled when the page first
loads. Then, JavaScript is used to enable the button.
The method can fail because some browsers will submit a form
when the return/enter key is clicked, which bypasses the
submit button.
Will Bontrager
©2007 Bontrager Connection, LLC Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.