You can achieve this using the window DOM object's window.opener property, which holds a reference to the parent window object model. After you have the parent window reference, you can access any functions, variables, or controls in the parent window.
Assume that parent window code (in the file ParentPage.htm) contains the following function and variable declaration:
var aVariable = 'some value';
//some code goes here
Additional code on that page opens a child window called ChildPage.htm
, which can call the Func1
function in the parent window using:
Note that you can also pass parameters to Func1()
To access the aVariable variable defined in the parent window code, enter the following:
Controls work the same way. To access a control with the id aControl
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.