1

I am trying to find a way to catch browser close event and call a server side code for database entry.

I have tried Javascript for window.onbeforeunload and body.onunload but unload event is fired every time the page navigates.

I need to catch only browser close event.

I am using asp.net with C# and my page have a master page.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Anil Raj
  • 19
  • 4
  • Please post your code, you can edit your question and paste in the additional code. Don't forgot to use the code-format button – Brian Webster Jul 19 '11 at 03:43
  • you are probably safer either changing your navigation so that it doesn't do entire page posts or just saving the data each navigation. I don't think there is another event other than either beforeunload or unload. – scrappedcola Jul 19 '11 at 04:01
  • Don't you need to check for onunload as well? What if the user enters some other URL directly and then closes the browser? (I guess off the top of my head I can't imagine a reason to care that the user is closing the browser that wouldn't also apply if they are navigating away from my page.) – nnnnnn Jul 19 '11 at 04:56

1 Answers1

1

Just use the code in one of these answers to call a function in your code-behind (that writes to DB)

Calling ASP.NET Code Behind function from JavaScript

Also, you can do client-side postbacks, explained on MSDN here

Community
  • 1
  • 1
Brian Webster
  • 30,033
  • 48
  • 152
  • 225