473,323 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

When browser is closed i want to call the web method of the webservice .. How can be done.??

Hello,

Can anybody please tell me how to trap the browser close event. when
that is fired I want to call the web method.

Thanks in advance.

Regards,
Gouri.
Jun 27 '08 #1
13 5611
<Go************@gmail.comwrote in message
news:ff**********************************@w8g2000p rd.googlegroups.com...
Can anybody please tell me how to trap the browser close event. when
that is fired I want to call the web method.
This is a fairly common question in this newsgroup.

Unfortunately, there is no reliable way to do this, because there is no
permanent connection between the webserver and the client browser. The
webserver has no idea what happens on the client browser until / unless it
receives another HttpRequest.

No doubt you'll receive other replies suggesting possible solutions with the
unload and/or beforeunload events - none of those works reliably...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #2
would it be possible with som javascript.. calling a webservice ?
--

Jesus Loves You
"Mark Rae [MVP]" wrote:
<Go************@gmail.comwrote in message
news:ff**********************************@w8g2000p rd.googlegroups.com...
Can anybody please tell me how to trap the browser close event. when
that is fired I want to call the web method.

This is a fairly common question in this newsgroup.

Unfortunately, there is no reliable way to do this, because there is no
permanent connection between the webserver and the client browser. The
webserver has no idea what happens on the client browser until / unless it
receives another HttpRequest.

No doubt you'll receive other replies suggesting possible solutions with the
unload and/or beforeunload events - none of those works reliably...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #3
Hello subtile,

Yep, you can use onbeforeunload of the body < body onbeforeunload="BrowerClose();">
and make ajax call to the web-service http://www.asp.net/ajax/documentatio...XTutorial.aspx

it's a common practice for this

---
WBR,
Michael Nemtsev [Microsoft MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
swould it be possible with som javascript.. calling a webservice ?
s>
s"Mark Rae [MVP]" wrote:
s>
><Go************@gmail.comwrote in message
news:ff**********************************@w8g2000 prd.googlegroups.com
...
>>Can anybody please tell me how to trap the browser close event. when
that is fired I want to call the web method.
This is a fairly common question in this newsgroup.

Unfortunately, there is no reliable way to do this, because there is
no permanent connection between the webserver and the client browser.
The webserver has no idea what happens on the client browser until /
unless it receives another HttpRequest.

No doubt you'll receive other replies suggesting possible solutions
with the unload and/or beforeunload events - none of those works
reliably...

--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #4
"Michael Nemtsev [MVP]" <ne*****@msn.comwrote in message
news:90**************************@msnews.microsoft .com...
>>No doubt you'll receive other replies suggesting possible solutions
with the unload and/or beforeunload events - none of those works
reliably...
Yep, you can use onbeforeunload of the body < body
onbeforeunload="BrowerClose();"and make ajax call to the web-service
http://www.asp.net/ajax/documentatio...XTutorial.aspx
No you can't. That will fire every time the page closes i.e. moving from one
page to another, not just when the browser closes...
it's a common practice for this
No it isn't - because it doesn't work...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #5
"subtile" <su*****@discussions.microsoft.comwrote in message
news:2D**********************************@microsof t.com...

[top-posting corrected]
would it be possible with som javascript.. calling a webservice ?
Not reliably... The JavaScript would fire every time the page closes i.e.
while moving from page to page within the same web app, and also during
postback etc...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #6
On Jun 11, 7:53 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
"subtile" <subt...@discussions.microsoft.comwrote in message

news:2D**********************************@microsof t.com...

[top-posting corrected]
would it be possible with som javascript.. calling a webservice ?

Not reliably... The JavaScript would fire every time the page closes i.e.
while moving from page to page within the same web app, and also during
postback etc...

--
Mark Rae
ASP.NET MVPhttp://www.markrae.net
Hi,

what Mark is said is absolutely true and i found a little workaround
to that...

here is the code i tried and found working on both IE and Firefox...

<body onbeforeunload="doUnload()" onmousedown="somefunction()">
<form id="form1" runat="server">
</form>
</body>
<script language="javascript" type="text/javascript">
var isClose = false;
function somefunction()
{
isClose = true;
}
function doUnload()
{
if(!isClose)
{
alert('window is closing');
}
}
</script>

this code is not perfect ... you guys can find some flaw ... still it
works... for most of the cases...

Best of luck

Munna

www.munna.shatkotha.com
www.munna.shatkotha.com/blog
Jun 27 '08 #7
this code fires on every page navigation (event postback) not just browser
close (though it will also fire on browser close).

this pattern is usually used to detect navigating away with unsaved data.
-- bruce (sqlwork.com)
"Munna" wrote:
On Jun 11, 7:53 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
"subtile" <subt...@discussions.microsoft.comwrote in message

news:2D**********************************@microsof t.com...

[top-posting corrected]
would it be possible with som javascript.. calling a webservice ?
Not reliably... The JavaScript would fire every time the page closes i.e.
while moving from page to page within the same web app, and also during
postback etc...

--
Mark Rae
ASP.NET MVPhttp://www.markrae.net

Hi,

what Mark is said is absolutely true and i found a little workaround
to that...

here is the code i tried and found working on both IE and Firefox...

<body onbeforeunload="doUnload()" onmousedown="somefunction()">
<form id="form1" runat="server">
</form>
</body>
<script language="javascript" type="text/javascript">
var isClose = false;
function somefunction()
{
isClose = true;
}
function doUnload()
{
if(!isClose)
{
alert('window is closing');
}
}
</script>

this code is not perfect ... you guys can find some flaw ... still it
works... for most of the cases...

Best of luck

Munna

www.munna.shatkotha.com
www.munna.shatkotha.com/blog
Jun 27 '08 #8
Hi bruce

Thanks for your reply i checked the code again after your replay

did you actually run the code i suggesed...?

i tested this code and found working...

the trick is simple

when mouse down in browser document traked it down.

Thanks

Munna
Jun 27 '08 #9
Hi

well may be the code need a little explanation...

the event onbeforeunload will fire each and every time browser
navigate,postback,close...
as bruce said is right...

but i have subscribed another code that is mousedown of the body...

each time the user do a operation using mouse in the document i
tracked it down in a variable

if a use close its browser there is no mouse down in document... thats
why i used this code...

<body onbeforeunload="doUnload()" onmousedown="somefunction()">
<form id="form1" runat="server">
</form>
</body>
<script language="javascript" type="text/javascript">
var isClose = false;
function somefunction()
{
isClose = true;
}
function doUnload()
{
if(!isClose)
{
alert('window is closing');
}
}
</script>

we can enhance this code ofcourse...
adding key press event to track down alt+f4 and etc...
Thanks

Munna
Jun 27 '08 #10
"Munna" <mu******@gmail.comwrote in message
news:27**********************************@e53g2000 hsa.googlegroups.com...
when mouse down in browser document traked it down.
Alt-{F4}
Alt F X
Alt-{LEFT} / Alt-{RIGHT}
Ctrl-{F5}

Etc...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #11
On Jun 11, 9:47 pm, "Mark Rae [MVP]" <m...@markNOSPAMrae.netwrote:
"Munna" <munna...@gmail.comwrote in message

news:27**********************************@e53g2000 hsa.googlegroups.com...
when mouse down in browser document traked it down.

Alt-{F4}
Alt F X
Alt-{LEFT} / Alt-{RIGHT}
Ctrl-{F5}

Etc...

--
Mark Rae
ASP.NET MVPhttp://www.markrae.net
Hi

Nice points by Mark...

well here is a simple javascript function that can be used to handle
key events...

this is just an example...

<body onbeforeunload="doUnload()" onmousedown="somefunction()">
<form id="form1" runat="server">
</form>
</body>
<script language="javascript" type="text/javascript">
var isClose = false;

//this code will handle the F5 or Ctrl+F5 key
document.onkeydown = checkKeycode
function checkKeycode(e) {
var keycode;
if (window.event)
keycode = window.event.keyCode;
else if (e)
keycode = e.which;

if(keycode == 116)
{
isClose = true;
}
}

function somefunction()
{
isClose = true;
}

function doUnload()
{
if(!isClose)
{
alert('window is closing');
}
}
</script>
Thanks

Munna
Jun 27 '08 #12
Hello Mark Rae [MVP],

yep, my mistake, somehow thought about page close :) not browser

actually, returned to OP quesion there is no 100% way to handle BROWSER closing

what OP could try is to handle top X/Y position and ALT+F4 key

function handleWindowClose()
{
if((window.event.clientX<0) || (window.event.clientY<0))
{
event.returnValue = "Closing by X button";
}
}

if (event.altKey==true && event.keyCode==0 )
{
alert("ALT + F4 ");
}
but thes wont handling closing page from toolbar or killing process from
task manager

---
WBR,
Michael Nemtsev [Microsoft MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
M"Michael Nemtsev [MVP]" <ne*****@msn.comwrote in message
Mnews:90**************************@msnews.microsof t.com...
M>
>>>No doubt you'll receive other replies suggesting possible solutions
with the unload and/or beforeunload events - none of those works
reliably...
Yep, you can use onbeforeunload of the body < body
onbeforeunload="BrowerClose();"and make ajax call to the
web-service
http://www.asp.net/ajax/documentatio...nsumingWebServ
icesWithAJAXTutorial.aspx
MNo you can't. That will fire every time the page closes i.e. moving
Mfrom one page to another, not just when the browser closes...
M>
>it's a common practice for this
MNo it isn't - because it doesn't work...
M>
Jun 27 '08 #13
"Michael Nemtsev [MVP]" <ne*****@msn.comwrote in message
news:90**************************@msnews.microsoft .com...
but thes wont handling closing page from toolbar or killing process from
task manager
Indeed. As has been mentioned, there is no 100% reliable way of doing
this...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #14

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

Similar topics

9
by: James Marshall | last post by:
I'm writing a library where I want to override document.write(), but for all document objects; thus, I want to put it in the prototype. I tried Document.prototype.write= my_doc_write ; but it...
2
by: NWx | last post by:
Hi, I have the following question: I have an app that uses user login/logout to identify users When user logon, I register logon time in a session variable When user logoff using the logout...
5
by: news.microsoft.com | last post by:
Hi everyone, I need some help (may be in the form of some sample code) for the subject question. I have an ASP.NET/C# application. I need to do quite a few tasks when the session ends. I...
9
by: tshad | last post by:
This is from my previous post, but a different issue. I have the following Javascript routine that opens a popup page, but doesn't seem to work if called from an asp.net button. It seems to work...
7
by: Boni | last post by:
Dear all, if I do myform.close() is it disposed or not? is it correct do following? myForm.close() Info=SomeControlOntheForm.text This seems to work. But am not sure that it is waterproof....
5
by: Homa | last post by:
Hi all, Can anyone give me some links about how to do an async web service call from aspx and display a temperary page before the web service returns? Thanks, Homa Wong
0
by: Matthew B. Lehrman | last post by:
I've googled all over the place trying to get answer to this question and some things have come close, but nothing quite. Asp.NET 1.1, Visual Studio 2003 I am writing Visual Basic apps. I can...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.