Introduction
We normally use long forms in our web pages that collect various types of information from the users/visitors. Since HTTP is a stateless protocol, there is no way for the server to know whether the user has completed filling up the form (unless he submits it) or just clicked any of the links (after filling half of the form) that takes him away from the form, thus discarding whatever he has typed till that point.
The Solution
With some small event handler facilities allowed for forms in Microsoft Internet Explorer and with the help of JavaScript, it would be easier to detect whether the browser is about to navigate away from the page containing the form, when there are contents filled by the user but yet pending to be submitted/postedback to the server.
Benefits
The user might have got interested on some links while he is half-way filling up the form and might have clicked it accidentally. If our form could alert him that this action of his could cause loss of whatever information that he has made in the HTML form, it would really be helpful to the user, particularly when the form contains large TextArea contents and user has typed significant amount of text in it.
This also relieves the server/developer to keep track of all links as LinkButtons or PostBack so that even if user clicks any of the links, whatever the user submits keep bouncing back and forth between the server and client till user decides to go forward submitting it or discard whatever he has typed into the form.
I hope that this small piece of JavaScript code would really be helpful in making HTML Forms really more friendly to the user.