473,836 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Capture the Browser Close event

I need to capture the event when the user closes the browser in my
application. I have some code in the session_End event, which works fine
when the session times out, but I need to end the session immediately when
the user closes the browser.

Does anyone know a way of doing this?

--
Thanks,

Bill Manring

Nov 19 '05 #1
4 4815
Hi Bill,

This is a tricky issue. The odds of you getting a succesful postback are
50/50
to say the best. I've had about a 95% success rate doing this:

1. Capture the onbeforeunload event in your body tag.
2. Incorporate the WebService.htc.
3. When the onbeforeunload event fires run a function that uses the
WebService.htc to tell a web service.

The browser will close before you get a response back but, about 95% of the
time, your web service will get hit and the user can successfully be logged
out. Note, that using the latest IE and Windows XP you will achieve almost
100% results. This is sort of complicated until you get the hang of it. If
this isn't something you want to try then here is another suggestion to
improve your chances but it might seem clunky to the user:

Have a main page that the user browses to. To run your app, they click a
button or link and then you use window.open() to create a new browser
window. This is the window your app runs from. Don't ask me why, but the
success rate of a postback from onbeforeunload seems to be much higher for
browsers launched from javascript than from browsers launched by the user.
Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Bill Manring" <Bi*********@di scussions.micro soft.com> wrote in message
news:46******** *************** ***********@mic rosoft.com...
I need to capture the event when the user closes the browser in my
application. I have some code in the session_End event, which works fine
when the session times out, but I need to end the session immediately when
the user closes the browser.

Does anyone know a way of doing this?

--
Thanks,

Bill Manring

Nov 19 '05 #2
Ken,

Thanks for the response. It turns out that my application already works the
way you suggested, with the app started from javascript, so your idea seems
to work. Everything would be fine except for the fact that the Session_Start
event seems to fire twice. I tried starting the main page directly, not
using the javas script but the Session_Start event still fires twice.

I am trying to keep track of the number of users logged in to our
application for purposes of enforcing a "concurrent user" license model. I
didn't want to wait until the session timed out to decrement the active user
count, which I am storing in a database. If I rely on the Session_Start
event to increment the user count, I am incrementing the count by two, which
is of course not desirable.

Any ideas on why the Session_Start event fires twice?

"Ken Dopierala Jr." wrote:
Hi Bill,

This is a tricky issue. The odds of you getting a succesful postback are
50/50
to say the best. I've had about a 95% success rate doing this:

1. Capture the onbeforeunload event in your body tag.
2. Incorporate the WebService.htc.
3. When the onbeforeunload event fires run a function that uses the
WebService.htc to tell a web service.

The browser will close before you get a response back but, about 95% of the
time, your web service will get hit and the user can successfully be logged
out. Note, that using the latest IE and Windows XP you will achieve almost
100% results. This is sort of complicated until you get the hang of it. If
this isn't something you want to try then here is another suggestion to
improve your chances but it might seem clunky to the user:

Have a main page that the user browses to. To run your app, they click a
button or link and then you use window.open() to create a new browser
window. This is the window your app runs from. Don't ask me why, but the
success rate of a postback from onbeforeunload seems to be much higher for
browsers launched from javascript than from browsers launched by the user.
Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Bill Manring" <Bi*********@di scussions.micro soft.com> wrote in message
news:46******** *************** ***********@mic rosoft.com...
I need to capture the event when the user closes the browser in my
application. I have some code in the session_End event, which works fine
when the session times out, but I need to end the session immediately when
the user closes the browser.

Does anyone know a way of doing this?

--
Thanks,

Bill Manring


Nov 19 '05 #3
Hi Bill,

Do your users need to log in? If so, are you using Forms or Windows
authentication? If you do then you could update a date/time field in your
database each time you authenticate them in the Application_Beg inRequest.
Then you could write a query to automatically set people as logged off when
they are inactive for a set number of minutes. I have web apps that track
online users like you but all of my apps use client-side scripting to
constantly hit the server behind the scenes every couple of seconds to prove
that they are still online. This requires javascript to be enabled in the
browser. Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Bill Manring" <Bi*********@di scussions.micro soft.com> wrote in message
news:A0******** *************** ***********@mic rosoft.com...
Ken,

Thanks for the response. It turns out that my application already works the way you suggested, with the app started from javascript, so your idea seems to work. Everything would be fine except for the fact that the Session_Start event seems to fire twice. I tried starting the main page directly, not
using the javas script but the Session_Start event still fires twice.

I am trying to keep track of the number of users logged in to our
application for purposes of enforcing a "concurrent user" license model. I didn't want to wait until the session timed out to decrement the active user count, which I am storing in a database. If I rely on the Session_Start
event to increment the user count, I am incrementing the count by two, which is of course not desirable.

Any ideas on why the Session_Start event fires twice?

"Ken Dopierala Jr." wrote:
Hi Bill,

This is a tricky issue. The odds of you getting a succesful postback are 50/50
to say the best. I've had about a 95% success rate doing this:

1. Capture the onbeforeunload event in your body tag.
2. Incorporate the WebService.htc.
3. When the onbeforeunload event fires run a function that uses the
WebService.htc to tell a web service.

The browser will close before you get a response back but, about 95% of the time, your web service will get hit and the user can successfully be logged out. Note, that using the latest IE and Windows XP you will achieve almost 100% results. This is sort of complicated until you get the hang of it. If this isn't something you want to try then here is another suggestion to
improve your chances but it might seem clunky to the user:

Have a main page that the user browses to. To run your app, they click a button or link and then you use window.open() to create a new browser
window. This is the window your app runs from. Don't ask me why, but the success rate of a postback from onbeforeunload seems to be much higher for browsers launched from javascript than from browsers launched by the user. Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Bill Manring" <Bi*********@di scussions.micro soft.com> wrote in message
news:46******** *************** ***********@mic rosoft.com...
I need to capture the event when the user closes the browser in my
application. I have some code in the session_End event, which works fine when the session times out, but I need to end the session immediately when the user closes the browser.

Does anyone know a way of doing this?

--
Thanks,

Bill Manring


Nov 19 '05 #4
Ken,

The users do login, when the application is isntalled they decide whether
they want to use Windows authentication or Forms authentication. I will
consider another stategy - keeping a table of logged in users in the
database. This might allow the possibility of users "sharng" a common login
to circumvent the license restriction, but maybe I'll live with that. I'll
give it some more thought.

Thanks for your help.

"Ken Dopierala Jr." wrote:
Hi Bill,

Do your users need to log in? If so, are you using Forms or Windows
authentication? If you do then you could update a date/time field in your
database each time you authenticate them in the Application_Beg inRequest.
Then you could write a query to automatically set people as logged off when
they are inactive for a set number of minutes. I have web apps that track
online users like you but all of my apps use client-side scripting to
constantly hit the server behind the scenes every couple of seconds to prove
that they are still online. This requires javascript to be enabled in the
browser. Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Bill Manring" <Bi*********@di scussions.micro soft.com> wrote in message
news:A0******** *************** ***********@mic rosoft.com...
Ken,

Thanks for the response. It turns out that my application already works

the
way you suggested, with the app started from javascript, so your idea

seems
to work. Everything would be fine except for the fact that the

Session_Start
event seems to fire twice. I tried starting the main page directly, not
using the javas script but the Session_Start event still fires twice.

I am trying to keep track of the number of users logged in to our
application for purposes of enforcing a "concurrent user" license model.

I
didn't want to wait until the session timed out to decrement the active

user
count, which I am storing in a database. If I rely on the Session_Start
event to increment the user count, I am incrementing the count by two,

which
is of course not desirable.

Any ideas on why the Session_Start event fires twice?

"Ken Dopierala Jr." wrote:
Hi Bill,

This is a tricky issue. The odds of you getting a succesful postback are 50/50
to say the best. I've had about a 95% success rate doing this:

1. Capture the onbeforeunload event in your body tag.
2. Incorporate the WebService.htc.
3. When the onbeforeunload event fires run a function that uses the
WebService.htc to tell a web service.

The browser will close before you get a response back but, about 95% of the time, your web service will get hit and the user can successfully be logged out. Note, that using the latest IE and Windows XP you will achieve almost 100% results. This is sort of complicated until you get the hang of it. If this isn't something you want to try then here is another suggestion to
improve your chances but it might seem clunky to the user:

Have a main page that the user browses to. To run your app, they click a button or link and then you use window.open() to create a new browser
window. This is the window your app runs from. Don't ask me why, but the success rate of a postback from onbeforeunload seems to be much higher for browsers launched from javascript than from browsers launched by the user. Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"Bill Manring" <Bi*********@di scussions.micro soft.com> wrote in message
news:46******** *************** ***********@mic rosoft.com...
> I need to capture the event when the user closes the browser in my
> application. I have some code in the session_End event, which works fine > when the session times out, but I need to end the session immediately when > the user closes the browser.
>
> Does anyone know a way of doing this?
>
> --
> Thanks,
>
> Bill Manring
>


Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8900
by: Derek | last post by:
Hello: I want to capture the event when a browser is closing, to give to the user the posibility of close or no this browser. When the browser is closing, this show a confirm window with two buttons: Accept and Cancel. When press the Accept button, the browser is closing, and when press the Cancel button, the browser isn´t closing. I have tried with the event onunload, but this closes first the browser, and
3
2741
by: Marcia Gulesian | last post by:
How can I capture the event when I click (focus) with the cursor anywhere in the page (that is, on a component or elsewhere). This event would occur in an I.E 5.5 or later browser.
4
5532
by: feng | last post by:
Hi, In our asp.net app, we need to capture the event when user close the browser window by clicking on the "x" button. But it doesn't seem to be as easy as it sound. Can someone show me how to do this? Thanks
5
4491
by: Uma Muthu | last post by:
Hello Everyone, How can I capture the event of closing the IE browser from within an ASP.NET web application? TIA, Uma
1
2801
by: Feng | last post by:
Hi, Need help on the following issue. We need to clean up some session specific backend resource when a user ends his session. We have a "Logoff" button on the pages that handles that. But the problem is that sometimes the user still closing their browser window by clicking on the "x" button of the window. This is quite a problem for us because it will leave unreleased resource on the server for an unpredictable period of time,...
6
3950
by: hemant.singh | last post by:
Hi all, I am trying to get a way by which I'll know exactly when user goes out of my site by clicking on close button in browser, So that w/e user click close button in browser, I can send a signal to server. This seems to be achievable with body unload events, but it is little too much, as even if user navigate within my site, this event will be generated, this can be avoided by handling onclick of each link, so that I'll know exactly...
1
10568
by: karthik juneni | last post by:
Hi all, Iam trying to capture windows closing event (i.e) when the user clicks on the "X" button i want to capture that event and want to update some values in the database.I tried two methods but iam getting problems with the two methods. First,one i tried with function Unload()
1
2777
by: destiny007 | last post by:
can any one help me to write code to capture selected text from a page but problem is that i am not able to decide where to call the functio.in this case the function is called in all cases of mouse out. here is the code <html> <HEAD> <SCRIPT LANGUAGE="JavaScript"> var captured = ""; function captext() {
4
3526
by: mbatestblrock | last post by:
I hope this makes some sense. My ultimate goal here is to execute a block of code if the mouse has not moved in a minute or so within the broswer. The machine I am running this on is for internal use only, so I have no fear of this not working in certain browsers, etc. I am no javascript pro but know enough to catch on if anyone is kind enough to help me out here. One of the only ways I could see this working is by having js check the...
0
9820
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9670
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9374
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6979
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5649
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4452
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3115
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.