I am having a problem submitting form on window unload if form information has been updated (album.updated = true):
window.onunload = function(evnt) { if (album.updated) { var save = confirm('Do you want to save changes?'); if (save) { alert('submitting form...'); document.edit_photos.submit(); } } }
It alerts "submitting form" but nothing is saved. But when I do a regular submit with a Save button - it saves information. What could be wrong here?
You cannot reliably interact with the server from the unload
event.
Sending an AJAX request might be a little more reliable, but should also not be relied on.
Instead, you should handle the onbeforeunload
event and return "You have unsaved changes";
.
This will instruct the browser to show a confirmation dialog allowing the user to cancel the navigation. (After which he can save manually)