PhpRiot
Download This Article
Download this article in PDF format with all listings and files.

Price: $5.00 AUD
(Approx. $4.10 USD)

More information
Browse Articles
Ajax (4), APC (1), CAPTCHA (1), CSS (3), Debugging (1), File Upload (1), Google (3), Google Maps (2), JavaScript (12), JSON (2), MVC (1), MySQL (7), onbeforeunload (1), OOP (1), PHP (28), PhpDoc (1), PostgreSQL (6), Prototype (11), Reflection (1), RFC 1867 (1), Robots (1), Scriptaculous (1), SEO (1), Sessions (1), SimpleXML (1), Smarty (5), SOAP (1), SPL (1), Templates (2), W3C (1), XHTML (1), Zend Framework (1), Zend_Search_Lucene (1)

PhpRiot Newsletter
Your Email Address:

Reminding Users to Submit Forms

The onbeforeunload Event

Modern browsers such as Internet Explorer, Firefox and Safari provide a JavaScript event called onbeforeunload. This event is triggered as soon as the user performs one of the actions described above (such as clicking a link). Browsers don't give quite as much control over this event as other events.

Essentially all the browser allows us to do is define a message which is then displayed automatically in a confirm box (that is, a box with "OK" and "Cancel" buttons). If the user clicks OK, the browser navigates to the new link. If the user clicks Cancel, then the browser stays where it is, allowing the user to then submit the form as normal.

Note that you cannot fully customize the displayed message. For instance, if you return the string Remember to submit the form!, the following message will be displayed in the confirm box (or something very similar):

Listing 1 The confirm message shown by the browser for the onbeforeunload event (listing-1.txt)
Are you sure you want to navigate away from this page?

Remember to submit the form!

Press OK to continue, or Cancel to stay on the current page.

In This Article


Additional Files