Capture The Result Of Window.onbeforeunload
I have a scenario, I have to save the changes when user clicks yes on window.onbeforeunload for that I need to submit the form and nothing should be happen when selected no. Any he
Solution 1:
You cannot use your own dialog in onbeforeunload
. The only thing you can do is return a string to be displayed (on some browsers). You cannot stop the browser from leaving, only the user can control that.
What you can do is the following:
window.onbeforeunload = function(){
return 'Are you sure you want to leave this page?';
};
This will ask the user if they want to leave or not. Then you can use the onunload
event to run a function when they leave. From there, you can make a "synchronous" AJAX request to submit the form.
window.onunload = function(){
var request = new XMLHttpRequest();
request.open('POST', '/SREPS/read.do', false);
request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
request.send(new FormData(readForm));
};
If you are using jQuery, you can do:
window.onunload = function(){
$.ajax({
url: '/SREPS/read.do',
type: 'post',
async: false,
data: $(readForm).serialize()
});
};
Post a Comment for "Capture The Result Of Window.onbeforeunload"