Skip to content

Instantly share code, notes, and snippets.

@danielgreen
Created June 13, 2013 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielgreen/5772915 to your computer and use it in GitHub Desktop.
Save danielgreen/5772915 to your computer and use it in GitHub Desktop.
Check for unsaved changes on a page

Develop an unobtrusive approach - if a form has the data-warn-unsaved="true" attribute then attach change event handlers to its fields. Do this for all such forms on the page. The handlers can set data-unsaved-changes="true" on the form.

Set window.onbeforeunload to a function that checks for any forms with data-unsaved-changes="true". If any such forms are found, return some text to be displayed as a prompt to the user.

When a form's contents are saved successfully, we want to set data-unsaved-changes="false". Define an event that can be invoked upon a successful save, in order to set the attribute's value?

Also see http://codethug.com/2013/02/01/knockout-binding-for-onbeforeunload/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment