PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

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