My web app is listening out for a page refresh.
I am attempting this by doing the following:
window.onbeforeunload = dialogBox;
The idea is to display a dialog box in order to prevent the page from refreshing until the user has made a choice from selecting either of the two buttons which determines whether they continue with the refresh or cancel the refresh.
Unfortunately the page displays the dialog box for a brief second and then continues with the refresh. Is there anyway that I can use 'onbeforeunload' to prevent the page from continuing with a refresh and show my dialog box?
Is it impossible to achieve using 'onbeforeunload'?
The code for my dialog box:
function dialogBox()
{
$("#dialog").html("");
$("#dialog").append("There appears to be some unsaved changes");
$( function() {
$( "#dialog" ).dialog({
modal: true,
buttons: {
Cancel: function() {
$(this).dialog( "close" );
},
"Save": function() {
},
},
})
});
}
Thank you all for your time.
Edit: I don't want to prevent a user from refreshing. Rather than display the generic pop-up box that appears when using onbeforeunload, I want to display a custom dialog box by calling a function which generates that. I am somewhat able to display that for a split second, and not permanently until someone has made a selection. That selection should then either continue with the refresh or half it entirely by selecting 'Cancel'.
If it helps, I am using Google Chrome as my web browser of choice.