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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change onUnload event dynamically

Status
Not open for further replies.

dvrobin

Programmer
Jun 4, 2001
40
US
Is there a way to change the onunload event for a page dynamically? I have a page that opens a popup. When the popup opens, I want to change the unload event for the opener to close the popup, if it is open still. I realize I could hardcode this into the opener page, but that page is actually being created by a different team and I have no control of their content. Also, they have a few hundred pages with the control that opens the popup. I have control over the popup and the code that fires when it opens. Thanks in advance for any help. I'll check in the morning and answer any questions then. Thank!!
 
well, heres a thought:

if you can get the name of the variable that they are using to store the new window in, or even the window name, use this (I think)

Code:
window.opener.onunload+='
window_name
Code:
.close()
' theEclipse
eclipse_web@hotmail.com
**\\||It was recently discovered that research causes cancer in rats||//**
 
Yeah, I've tried that. Unfortunately, it doesn't appear to work. I've also tried document.body.onunload. For some reason, those statements don't cause any Javascript errors, but they don't actually change the onunload event information.
 
Have you tried anything like parent.window.onUnload? I'm not sure if that will work or not, but I can change things like the status bar message and the like by calling parent.window. Steve Kiehl
webmaster@nanovox.com
 
Okay, I got it. Thanks to Webmonkey.com for the info. The correct syntax is as follows:

window.onunload=MyFunction;

Note: MyFunction is the name of the function you want to call for the onunload event. You must leave the quotes off and no parentheses either. My guess is that this is more like a pointer to the function. I know JavaScript doesn't use pointers, but that's the best explanation I have. Thanks for the help!
 
If I understand it correctly, your code opens the window, am I correct?!
If so, try giving the popup window a certain namn such as PopUpWin and in the document.onunload event just call a function

function name()
{
if(PopUpWin)PopUpWin.self.close(); // or should it bePopUpWin.close()??
} My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
Here's the code that I used:

var strTemp = new String();

function doStartApp(){

//Grab the current onunload event handler
var pos;
//If the opener page has an onunload event handler
if(parent.window.onunload)
{
strTemp = parent.window.onunload.toString();
pos = strTemp.indexOf("{")+1;
//Parse out the code inside the function.
strTemp = strTemp.substr(pos,strTemp.length-(pos+1));
}
//Reset the function to our function.
//Our function will eval strTemp and execute it as well.
parent.window.onunload=closeApp;

OpenWin();
}

function OpenWin(){

theWindowName = "MYWINDOW"
strHeight = screen.height - 60
strWidth = screen.width - 12
featureStr = "height=" + strHeight + ",width=" + strWidth + ",left=0, top=0, menubar=no, scrollbars=no, resizable=yes";
winContent = '/MySite/MyFile.htm';
MYWINDOW = launch(winContent, theWindowName, featureStr, "test.asp");
return;
}

function closeApp(){

if (window.MYWINDOW){
if (MYWINDOW && !MYWINDOW.closed){
alert('closing App...');
MYWINDOW.close();
}
}
eval(strTemp);
return;
}

The doStartApp function is called on the click event of a button that is on another team's page. I grab the function that they are using (if any) for the unload event and convert it to a string. I then strip off the "function anonymous {" and the "}" that wrap their code. The browser evidently adds this as its not in the original code.

Once I do that, I store the result in a global variable and change the onunload handler to point to my function. My function first closes the App window and then executes the code that was originally intended to be the unload code.

The main advantage here is that I can change the onunload event of the calling window dynamically! And I can still execute the code that the creator of that window originally wanted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top