Click to See Complete Forum and Search --> : close window delete cookies


marko_one
03-25-2003, 10:14 AM
Hi All,
I need a way to detect that the user has clicked the close button ( X ) and run a script which can clear cookies. I already know how to clear the cookies (using vbscript response.cookies) but I don't know how to run the clearcookies script if the user presses the the close button

such as

if document.closebutton
{

document.location.href="clearcookies.asp";
}

can anybody help me out

many thanks

Marko

khalidali63
03-25-2003, 10:23 AM
Use the window event

onunload

.the above will be triggered if user closes the window.

Cheers

Khalid

requestcode
03-25-2003, 11:22 AM
Just a note that the onOnLoad event will not work with Netscape version browsers when you close the window using the "X" button to close the window.

marko_one
03-25-2003, 12:41 PM
Thanks for the reply, but there is a small problem,

I only want to run a function/procedure if the user closes the window. I dont want it to run if the user is directed to another page from within the original page from for example a response.redirect("NextPage.asp") or an <a href="MyPage.asp">My Page</a>

Thanks

MArko.

marko_one
03-25-2003, 12:49 PM
The code below is like what I am doing, the page will always. It is actually a login page and cookies are set, so the clearcookie() function should be called if the user closes the page but not if the user goes through the site as the details of the logged user need to stay alive until the user leaves, at which point they are reset.

<html>
<head>
<script language=javascript>
function clearCookie()
{
alert ("You presssed the close button");
}
</script>

</head>
<BODY onunload="clearCookie();">
on success <%Response.Redirect("NextPage.asp")%>
</body>
<html>


Thanks

Marko

Phil Karras
03-25-2003, 01:27 PM
Use onUnload to call a function, use the button (or whatever) in the parent window to also call a function that sets a var prior to redirecting the child window.

The function called by onUnload then first checks the var to see where the command to redirect or close came from. If from the parent skip the delete stage, if from the child run the delete stage. (Don't forget to reset the var before proceding.)

I think you get the idea now.

Also, I've noticed no problems in NS7.0 with closing with the [X] button and having onUnload work.