gmustudent gmustudent - 5 months ago 26
Javascript Question

Clear Onbeforeunload

I have a textarea that is submitted with

ajax
that uses onbeforeunload to warn the user that they have not submit the textarea if they try to close the browser. The problem is I need to somehow clear the onbeforeunload within the success of the
ajax
submit so that onbeforeunload knows that the user has submitted the form successfully. Is there a way to clear the onbeforeunload with a line of code, similar to a clearTimeout or clearInterval? I'll show the onbeforeunload code I currently have even though I don't think it matters much b/c I'm looking for a different function to clear this. No frameworks please. Thank you.

Function call

unsavedChangesWarning();


Onbeforeunload Function

function unsavedChangesWarning(){
window.onbeforeunload = function(){
return 'If you close this page you will lose your stride update!';
};
}

Answer Source

window.onbeforeunload = null will clear it. Just add this before you try to redirect the user anywhere.