When creating data entry forms in ASP .NET, the JavaScript onbeforeonload event can add a huge improvement to the end user experience. This nifty little event is fired before a page is unloaded (duh) and will keep users from navigating off of a page (and even from closing their browser) when they have unsaved changes on a data entry form.

The onbeforeunload event behaves a bit oddly for an event. Basically in your event handler function, if you return anything, the event will pop up a message box with whatever you returned sandwiched between “Are you sure you want to navigate away from this page?” and “Press OK to continue, or Cancel to stay on the current page.” Here is an example:
 
 
If, however, your function doesn’t return anything, no warning will show up (but the page is removed from cached memory so you can’t navigate back to it.) This provides a very nice tool for alerting users. All your function needs to do is to check if data has been edited and if so, return an appropriate string. The window prompt and warning will then be handled for you by the onbeforeunload event.
 
The function is extremely easy to use. In the “<script type="text/javascript">“ section at the end of an ASPX page you just need to assign a function to the “window.onbeforeunload” event. Below is a complete snippet (the JavaScript variable bEdited is set outside of this snippet to true if data on the form has been edited.)
 
<script type="text/javascript">
    var bEdited=new Boolean();
   
    function UnLoadWindow() {
        if (!(bEdited)) {
           return
        }
        return 'DMC strongly recommends NOT closing this window yet.'
    }
 
    window.onbeforeunload = UnLoadWindow;
</script>

 

Share this page:

Comments

-->
# suzanne
Sunday, September 06, 2009 1:31 AM
Hi,

How can i make that code redirect to a webpage?

Thanks.
# Danny Budzinski
Friday, September 11, 2009 11:34 AM
Try putting this into your UnLoadWindow() function:

window.location = "http://www.google.com/"

I haven't tested if that works in the onbeforeunload event, but it is worth a shot.

-->
# aqeel ansari
Thursday, November 19, 2009 12:52 AM
Thanks a lot !!
It really help me.
-->
# djzoc
Friday, August 13, 2010 11:11 AM
hello there, nice script i really search this, but how i can setap this to work just if the user close the browser/tab not if he click some another link on my page
-->
# mikey
Sunday, January 08, 2012 9:35 PM
I have no code snippet to post here as it would be too long, but basically if it helps what I did to get around djzoc's question is that all the links on the website that I maintain I have filtered through a javascript function which redirects them based on their name. at the loading of the main.js file I have a variable which I set to 1 and when they click on a link that filters through that javascript function it sets that variable to zero, and only if that variable is nonzero does the window.beforeunload function get triggered. Therefore only when navigating off or closing the window.
-->
# anoop
Wednesday, March 28, 2012 10:23 AM
Hi Danny,
i am facing an issue, I have multiple buttons on my page once i cancel this popup button stops working.

Post Comment

Name (required)

Email (required)

Website

Enter the code shown above in the box below