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

  • Thread starter Thread starter Gouri.Mahajan7
  • Start date Start date
G

Gouri.Mahajan7

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.
 
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/documentation/live/tutorials/ConsumingWebServicesWithAJAXTutorial.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


s> would it be possible with som javascript.. calling a webservice ?
s>
s> "Mark Rae [MVP]" wrote:
s>
 
[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...

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
 
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 said:
[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...

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
 
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
 
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
 
Alt-{F4}
Alt F X
Alt-{LEFT} / Alt-{RIGHT}
Ctrl-{F5}

Etc...

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
 
Back
Top