How To Prompt Users Of Unsaved Changes If They Try To Leave The Webpage
Possible Duplicate: How do I stop a page from unloading (navigating away) in JS? Prompt user for unsaved changes when leaving webpage I have a php form which i designed in dream
Solution 1:
onbeforeunload
fires before navigation actually begins, and you can stop the navigation using a rather unique method: simply return the string you'd like displayed in a dialog, and the browser will ask whether or not the user really wants to leave the page.
If you don't return a string, the browser continues navigation normally. You could use this behavior, for example, to only show a prompt if the user has unsaved changes.
window.onbeforeunload = function(e) {
if (unsavedChanges) return'Dialog text here.';
};
You can only stop navigation by returning a string and letting the browser prompt. Calling alert
or confirm
is actually prohibited in onbeforeunload
, and in onunload
, you have no facility to actually stop navigation.
Solution 2:
Use unload method with "on" api of jquery.
var flag = true; // set this var according to your use.
$(window).on('beforeunload', function(){
if(flag) {
return"It looks like you have input you haven't submitted."
}
});
Post a Comment for "How To Prompt Users Of Unsaved Changes If They Try To Leave The Webpage"