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

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.
 
M

Michael Nemtsev [MVP]

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>
 
M

Munna

[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
 
B

bruce barker

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
 
M

Munna

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
 
M

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
 
M

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top