We help IT Professionals succeed at work.

Trapping the close of IE in ASP.Net

vj_mi asked
Medium Priority
Last Modified: 2013-12-17

I have a web app using ASP.Net 3.5 and C#. Now when the user closes the browser, I want to trap that and make an entry in the log file. How do I achieve this?

Watch Question

The close event of a web page occurrs client side.
So you can do things like this:

<body onunload="alert('Goodbye');">
But obviously you put in some other javascript code which communicates with the server to enable you to be aware of that event and log somethign somewhere.
You may also want to consider Session timeout instead?
Top Expert 2009
That's a bit tricky, because the browser may not send the close window event, or execute code in it if it involves a new request to the server. You can try something like this:

<script language="JavaScript">
 function doUnload(evt)
    var e = (window.event) ? window.event : evt;
    if (e.clientX < 0 && e.clientY < 0){
        // send request to server using XHR
 <body onunload="doUnload(event)">

It's reported to work in IE6, not sure of IE7, as it is non-standard behavior to be able to do something if the user exits the application. In FF / Opera it definitely does not work.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2009

The clientX/Y coordinates are used to track whether the window is closed, or whether refresh is hit (or any other button). It will not work if the window is minimized. It will not work if the window is closed from the task manager. To track these kind of events, you have to resort to something else.

That might be using a ticker. This ticker sends a signal every 5 seconds or so (again with using the XHR object) and once this ticker stops you can act as if the window was closed. A refresh action can be tackled in another way to prevent unnecessary mistakes due to latency. But, in short, it is not trivial. This second approach will work cross browser though.
I just tried it in FireFox and it worked fine.  Also works in IE8.
I think you are better of working with Session timeouts.
Its like asking who is watching my tv programme.  They might have switched channel so you could be aware of that but wwhat if they  just left the room or fell asleep.  Its a bad science really and you will never get a satisfactory solution I don't think.
Top Expert 2009

Alternatively, and way easier, if you use a session, you can use that. At some unique id on every URL. On the session_end event you can set an entry in the logfile "session abandoned", with the ID of the url. Then, if the user does something on the page and the Session_start is called again, check the referrer. If this unique id matches an earlier id of "session abandoned" then the user did not close the browser and open it again. If there's no match, the browser was closed (or the tab and a new tab was opened).

If you need the time, you can get as precise with +/- the expiry time of the session. If you want it more exact you have to use one of my previous suggestions.

Btw: just checking on the onUnload event will not help you, because it is called in many more cases then only closing the browser.
Top Expert 2009

> I just tried it in FireFox and it worked fine.  Also works in IE8.

What did you try? The onUnload event? It will also work on refresh, moving to another page, hitting the back button etc. The window close event may or may not be called, depending on your security and javascript settings. Let alone sending a new request to the server once the user requested to close the browser.

I know this is considered "bad design" and you "should not" code like this, after all, anything HTTP is stateless... Bearing that in mind, I think your second suggestion, Sessions (along with mine) will give you more control, albeit less precise.
Yep ok fair points.
I would never recomend coding this way but if someone insists they want to you can only try and help.
Top Expert 2009

Btw, a point of interest if you (the OP) are only targeting IE, you should consider the OnBeforeUnload event, which happens before the page is really gone and you may be able to send something to the server...
Top Expert 2009

@vj_mi, thanks for closing the question, but why do you give a B grade if you didn't first give us some opportunity to help you out to satisfy you completely? You didn't comment at all on the answers. And the same is what you did here: http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_24157678.html.

For grading questions and helping experts with comments into helping you, see the guidelines here: http://www.experts-exchange.com/help.jsp (under "Asking Questions"). Here's an excerpt:
What's the right grade to give?        
Grading at Experts Exchange is not like school. It's more like the "10-point Must" system in professional boxing; in other words, an answer is worth an A, unless it doesn't resolve your issue. If it requires you to do a little more research, or figure out one more piece of code, then it's worth a B. If you think it's not worth a B, the custom is to offer the Experts an opportunity to earn a better grade.
Giving a higher grade has no impact on your Available Points.
Make sure to give experts the opportunity to answer your questions to your satisfaction by giving them feedback and their comments. We can't read minds, we need your help to help you.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.