onbeforeunload event - are you sure that’s what you want to do?

`onbeforeunload` is a black sheep in the JavaScript event family. It’s nonstandard and yet supported all major browsers except Opera. You’ve seen it before in Gmail or Wordpress. When you try to close a draft that hasn’t been saved you are asked to confirm your action - that’s `onbeforeunload` in action.

Using jQuery we can duplicate this effect. The gist of the idea is to use a boolean called `dirty` to track changes to input elements. If `dirty` is set we’ll use the `onbeforeunload` event to confirm that user’s action:

Check out Stackoverflow for an introduction to the `onbeforeunload` event.

Leave a Reply