Click here to Skip to main content
14,302,993 members

Save Changes on Close of Browser or When Exiting the Page

Rate this:
4.36 (23 votes)
Please Sign up or sign in to vote.
4.36 (23 votes)
24 Sep 2007CPOL
This article will show how to save changes on the close of the browser or when exiting a page.


This article describes how to implement the functionality of saving changes on browser close or navigating away from the current web application. It also discusses the various ways that the user can exit a page and how to save data on those events.


Many have been trying to implement the functionality of saving data on closing the browser. I was also one of them trying to find a way to implement the same. I had been to many forums and discussion boards, but couldn't find a way to implement this. I finally landed on a piece of JavaScript code (attached below) to trap the close event of an IE window, which actually helped me in implementing this functionality.

Using the Code

Navigation in a site can happen in two ways. Following is a list of actions by which users can navigate:

  • Navigating to pages using controls provided within the site.
  • Navigating to pages using browser controls, shortcut keys, or right click options.

Following are ways through which the user can navigate away from a page. All these are covered in this solution.

Navigation or postback using controls available in the application:

  • Image buttons
  • Link buttons
  • Submit buttons
  • Server controls with the "Autopostback" option set to true

Navigation through user actions, browser buttons, shortcut keys, closing the browser. Following is a list of ways by which this is achieved:

  • Back button on the browser Toolbar
  • Refresh button on the browser Toolbar
  • Close button on the browser Toolbar
  • Home button on the browser Toolbar
  • ALT + F4 to close the browser
  • CTRL + F4 (for tabbed browsers)
  • F5 or CTRL + R or Right click for refresh
  • ALT + Home button (to navigate to client's Home page)
  • Click backspace (alternative for Back button)
  • Typing a URL and navigating to a different page
  • Selecting a URL from the Favorites button
  • Right click and select Refresh

Identifying the different actions and enabling save for each action is a tedious process. The save functionality can be implemented in a simpler way using the JavaScript onbeforeunload event.

Reasons to use onbeforeunload

The onbeforeunload event (which is called before the unload event) is called when:

  • Navigating to another page (as mentioned above)
  • Postback due to a control event
  • Using controls on the browser
  • Closing the page

Thus, onbeforeunload is the appropriate event at which the save functionality can be implemented.

Implementation of the Save functionality

To differentiate between a user initiated event and a control initiated event, an attribute named tag is added with a value "DonotCallSaveonLoad" to all the known controls in the application, using which the Save method can be called without prompting the user to save the page.

Screenshot - Attributes-Add.png

event.srcElement will set the object of the control to the variable currentElement which initiated the postback. This will be set on the onClick event of the control.

Note: The onClick event is the only point where the Control object can be retrieved because the object of the control is destroyed when the process reaches the onbeforeUnload event.

The onbeforeUnload event calls the HandleOnClose function which checks if the variable currentElement has a value or not.

Screenshot - body-load.png

The JavaScript function HandleOnClose checks for this attribute tag of the currentElement object. If currentElement is null or the value of the tag attribute of currentElement is not DonotCallSaveonLoad, then it calls the function FunctiontoCallSaveData; otherwise, the save functionality on the code-behind is called.

Screenshot - Handle-Close.png

The implementation of the function FunctiontoCallSaveData is the same as previously sent, which is as follows:

Screenshot - Save-Data_Function.png

form.submit will in turn call Page_Load in which the Save functionality can be called. Error handling can also be performed for this functionality. The same is shown in the code snippet below:

Screenshot - Page-Load.png

Points to Note

This functionality cannot be debugged using breakpoints. This functionality can be verified by checking if the data has been updated in the data source. In the attached demo, there is a SQL script which creates a temporary table to which data is stored on the close of the browser. Please make necessary changes to the function SaveDataonPage on the .vb file as per your convenience. Exception Handling is implemented using Enterprise Library 2.0. Customizations can be done to its Exception Handling mechanism also.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Abishek R Srikaanth
Architect Spozzle
India India
A highly experienced and influential Technical Manager / Team Leader and Architect, an inspirational leader who has effectively managed, developed and motivated internal and external teams. Strong ability to quickly understand and analyze business strategies ensuring technology solutions meet business requirement, to quickly adapt to all new technologies and challenges.

My attention to detail, high technical aptitude and hands on approach together with a high degree of motivation and professionalism would make me an asset to any progressive organization or demanding position within IT industry.

What do I do?

I work at Spozzle( as a Software Architect and currently working on the following technologies.

Web Technologies using

XCode 4 and IPhone Development
HTML 5 and CSS
Android Development

My Experience

I have spent my last 7 years working on .NET Technology building application both on the Web and Windows.

I provide Technical consultations to people on

- .NET Development, Best Practices and Issues
- Best Practices on Web based Architecture and Hosting applications on Cloud.

I have not moved on to PHP, Java and Mobile Development. I now work at Spozzle developing really cool application for the Sports Fanatics on Facebook. Checkout

Comments and Discussions

Questionauto save data on exiting the page web application Pin
prerana@200922-Jun-15 5:47
memberprerana@200922-Jun-15 5:47 
GeneralMy vote of 5 Pin
Prasyee30-Oct-12 19:54
memberPrasyee30-Oct-12 19:54 
QuestionSolution for substitute for onbeforeunload event that does not works for ie8 Pin
nawabprince5-Jul-12 21:46
membernawabprince5-Jul-12 21:46 
GeneralMy vote of 5 Pin
BhabeshM20-Nov-11 22:07
memberBhabeshM20-Nov-11 22:07 
GeneralMeta Refresh Tag Pin
sharmilag29-Dec-10 9:26
membersharmilag29-Dec-10 9:26 
GeneralIn browser of some systems it doesent work Pin
snaseraldini29-Sep-10 22:37
membersnaseraldini29-Sep-10 22:37 
GeneralDoesn't work for some controls~~ Pin
JPASP20088-Mar-10 20:20
memberJPASP20088-Mar-10 20:20 
GeneralThanks Pin
Falguni Barik11-Sep-09 3:30
memberFalguni Barik11-Sep-09 3:30 masterpage Pin
naveenIIT12-Jan-09 17:39
membernaveenIIT12-Jan-09 17:39 
GeneralRe: masterpage Pin
fuzzballb30-Oct-09 0:24
memberfuzzballb30-Oct-09 0:24 
GeneralServer Postback Issue... Pin
Rohan Christian2-Oct-07 12:08
memberRohan Christian2-Oct-07 12:08 
GeneralRe: Server Postback Issue... Pin
Abishek R Srikaanth3-Oct-07 21:31
memberAbishek R Srikaanth3-Oct-07 21:31 
GeneralRe: Server Postback Issue... Pin
Rohan Christian4-Oct-07 8:45
memberRohan Christian4-Oct-07 8:45 
Generalmissed some events Pin
SkullKiller25-Sep-07 10:15
memberSkullKiller25-Sep-07 10:15 
AnswerRe: missed some events Pin
Abishek R Srikaanth3-Oct-07 21:27
memberAbishek R Srikaanth3-Oct-07 21:27 
GeneralIncomplete (defective) code Pin
fwsouthern12-Sep-07 23:10
memberfwsouthern12-Sep-07 23:10 
GeneralRe: Incomplete (defective) code Pin
Abishek R Srikaanth24-Sep-07 20:18
memberAbishek R Srikaanth24-Sep-07 20:18 
GeneralVery helpful Pin
samerh11-Sep-07 2:04
membersamerh11-Sep-07 2:04 
GeneralRe: Very helpful Pin
ricobano23-Oct-07 5:10
memberricobano23-Oct-07 5:10 
GeneralNice Pin
Pooya Musavi7-Sep-07 10:05
memberPooya Musavi7-Sep-07 10:05 
AnswerRe: Nice Pin
Rodrigo Rodriguez12-Sep-07 4:48
memberRodrigo Rodriguez12-Sep-07 4:48 
Questiondoesn't work with FireFox Pin
barbod_blue1-Sep-07 20:50
memberbarbod_blue1-Sep-07 20:50 
AnswerRe: doesn't work with FireFox Pin
Abishek R Srikaanth3-Sep-07 20:51
memberAbishek R Srikaanth3-Sep-07 20:51 
GeneralRe: doesn't work with FireFox Pin
dinesh12345815-Nov-10 23:35
memberdinesh12345815-Nov-10 23:35 
Questiondoesn't work with FireFox Pin
barbod_blue1-Sep-07 20:49
memberbarbod_blue1-Sep-07 20:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 28 Aug 2007


114 bookmarked