Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Window Unload

Status
Not open for further replies.

jl8789

MIS
May 22, 2003
293
US
I want to capture the event triggered by the X when closing a window. I want to have the cancel button on my form happen when the x is used to close the window.

I tried unload, but that happens even when I hit the OK button on my window.
 
"I want to have the cancel button on my form happen when the x is used to close the window." I'm totally at loss here as to what you are trying to say. Could you be more specific.
 
There is an X ie. close button on every window in the upper right hand corner. I know unload is one event that I can use while a window is closing to do some sort of user-defined function. I have code behind my cancel button that I want to execute if the user uses the X to close the window. What is the method or property I have to use to be able to do this?
 
you can write a javascript function and call when a user clicks on the Cancel button.

something like this:

function checkWindow(){
if(window.closed){
//things that you want to accomplish.
}else{
//the default execution.
}
}
 
sorry..I just read your post again and disregard what I just told you.
 
I don't think there is a way to distinguish between closing the window and leaving the page via an onunload element in the body tag. However, you can use a hidden variable on your page to flag whether or not the page is closing or just being redirected. Do something like this:
Code:
<script language='Javascript'>
function closeFunction() {
   if (blahForm.blahHidden.value == 0) {
      //run your cancel button function
   }
}
function buttonFunction() {
   blahForm.blahHidden.value = 1;
   window.location = 'blah.html';
}
</script>
<body onunload='closeFunction()'>
<form name=blahForm value=0> 
<input type=hidden name=blahHidden>
<input type=button name=blahButton onclick='buttonFunction()'>
</form>
</body>
As you can see above, when the page closes or redirects it runs closeFunction(). However, if the closeFunction() is called because the button redirected it to another page, the hidden variable has been flagged to 1, so the cancel button code would not execute. Just make sure that you make every link or redirection on your page flag the hidden variable to 1, otherwise it will trigger the cancel button code.

-kaht

banghead.gif
 
I am closing the window whether I hit OK or cancel. This is the problem... I want to use the cancel button if they use that x to close the window
 
whoops, typo:

<form name=blahForm value=0>
<input type=hidden name=blahHidden>

should be:

<form name=blahForm>
<input type=hidden name=blahHidden value=0>


-kaht

banghead.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top