535Chapter 23 .The Form and Related Objects Tip (Web host sites)

535Chapter 23 .The Form and Related Objects Tip The best opportunity to change the properties of a FORM element object is in a function invoked by the form s onSubmit event handler. The modifications are performed at the last instant prior to actual submission, leaving no room for user- induced glitches to get in the way. Buttons in forms A common mistake that newcomers to scripting make is defining all clickable buttons as the submit type of inputobject (). The Submit button does exactly what it says it submits the form. If you don t set any METHOD or ACTION attributes of the

tag, the browser inserts its default values for you: METHOD=GETand ACTION=pageURL. When you submit a form with these attributes, the page reloads itself and resets all field values to their initial values. Use a Submit button only when you want the button to actually submit the form. If you want a button for other types of action, use the button style (). A regular button can invoke a function that performs some internal actions and then invokes the FORM element object s submit() method to submit the form under script control. Redirection after submission Undoubtedly, you have submitted a form to a site and seen a Thank You page come back from the server to verify that your submission was accepted. This is warm and fuzzy, if not logical, feedback for the submission action. It is not surprising that you would want to recreate that effect even if the submission is to a mailto: URL. Unfortunately, a problem gets in the way. A common sense approach to the situation calls for a script to perform the submission (via the form.submit() method) and then navigate to another page that does the Thank You. Here is such a scenario from inside a function triggered by a click of a link surrounding a nice, graphical Submit button: function doSubmit() { document.forms[0].submit() location.href = thanks.html } The problem is that when another statement executes immediately after the form.submit() method, the submission is canceled. In other words, the script does not wait for the submission to complete itself and verify to the browser that all is well (even though the browser appears to know how to track that information given the statusbar feedback during submission). The point is, because JavaScript does not provide an event that is triggered by a successful submission, there is no sure-fire way to display your own Thank You page. Don t be tempted by the window.setTimeout() method to change the location after some number of milliseconds following the form.submit() method. You cannot predict how fast the network and/or server is for every visitor. If the submission does not complete before the timeout ends, then the submission is still canceled even if it is partially complete. It s too bad we don t have this power at our disposal yet. Perhaps a future version of the document object model will provide an event that enables us to do something only after a successful submission. FORM
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

Leave a Reply