Monday, January 19, 2009

Invoke a Parent Window Function from Another Window in JavaScript

Additional code on that page opens a child window called ChildPage.htm, which can call the Func1 function in the parent window using:
window.opener.Func1();
Note that you can also pass parameters to Func1() in the same way you do for any other JavaScript function.
To access the aVariable variable defined in the parent window code, enter the following:

window.opener.aVariable;
Controls work the same way. To access a control with the id aControl, use:
window.opener.aControl;
One caveat--window.opener can be null, so when you use it, always check to ensure that it is not null, and also ensure that the parent window is not closed, by using the window.opener's closed property. You can check both with one line of code:
if(window.opener != null && !window.opener.closed)
{
//some code goes here.
}