问题
In my program, if a user tries to leave a page, he'll receive a dialog box asking if he is sure he wants to leave.
How should I implement the 'cancel' option if the user chooses not to leave the page?
Source javascript code:
$(window).unload(function(){
var c= confirm ("Are you sure?");
if (c){
alert("Thanks. Good luck!");
}
else{
????
}
});
回答1:
window.onbeforeunload = function() {
return 'You have unsaved changes!';
}
many question about this in stackoverflow How can I override the OnBeforeUnload dialog and replace it with my own?
JavaScript + onbeforeunload
回答2:
jQuery(window).on('beforeunload',function(){
var value = navigateAway(isDirty);
if(value=="changed")`enter code here`
{
if(jQuery("#saveCheck").val()=="false")
{
return "<?php echo JText::_('OVR_WANT_TO_LEAVE');?>";
}
}
});
来源:https://stackoverflow.com/questions/9564663/how-can-i-prevent-a-page-unload-with-jquery