• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

Trapping the close of IE in ASP.Net

Hello,

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?

Regards,
MI
0
vj_mi
Asked:
vj_mi
  • 6
  • 4
1 Solution
 
daveamourCommented:
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?
0
 
abelCommented:
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
    }
 }
 </script>
 </head>
 <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.
0
 
abelCommented:
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.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
daveamourCommented:
I just tried it in FireFox and it worked fine.  Also works in IE8.
0
 
daveamourCommented:
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.
0
 
abelCommented:
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.
0
 
abelCommented:
> 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.
0
 
daveamourCommented:
Yep ok fair points.
I would never recomend coding this way but if someone insists they want to you can only try and help.
0
 
abelCommented:
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...
0
 
abelCommented:
@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.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now