Monday, 24 April 2006
HowTo: cancel an onbeforeunload event
if you're like me and use the onbeforeunload event in a web page to display a reminder to a user who might unintentially lose work by browsing away from the page, then you might be trying to find a way to cancel the event in the case where the user clicks the save button (they obviously don't need reminding now that they have clicked the button...)
on the internet you will find many people suggesting you use something like
window.onbeforeunload = null
Page.ClientScript.RegisterStartupScript(typeof(String), "ConfirmClose", @"
window.onbeforeunload = confirmExit;
if(postback == false)
return ""Please don't leave this page without clicking the 'Save Changes' or 'Discard Changes' buttons."";
then my save button contains the following aspx markup:
this sets the 'postback' variable to true, which gets picked up in the confirmExit() function, having the effect of cancelling the event.
hope you find this useful. it is tested and works in IE6 and FF 18.104.22.168.
Monday, 24 April 2006 17:10:13 (GMT Daylight Time, UTC+01:00) Asp.Net