0

How to trace on asp.net that user clicked on the browser close button.i tried with page unload event but its not fired when user clicked on the browser close button.I need to perform some operation to remove the temp file that time.

2 Answers2

0

Unfortunately that's not possible from the server side, as the browser doesn't contact the webserver in any way when the window is being closed.

You'll need to use JavaScript in order to trigger some kind of AJAX request when the window is being closed.

Using plain JavaScript try:

window.onbeforeunload = function() { /* your ajax request here */ }

or if you're using JQuery:

jQuery(window).bind("beforeunload", function() { /* your ajax request here */ })

Note that this will trigger everytime a page is unloaded, whether you actually close the page or if you post a form etc.

Hope that helps!

westin
  • 326
  • 1
  • 4
0

You can use Javascript to listen to the onunload event and then use __doPostBack to trigger an event on the server side.

See: how to use dopostback

Community
  • 1
  • 1
Blachshma
  • 17,097
  • 4
  • 58
  • 72