Questions and Answers on Chrys`s Approach to Active Client Pages
In this eleventh part of a twelve-part series on active client pages (ACP), I give you more factors that gave rise to the Window Phase of Chrys’s Approach. We continue with the questions and answers more or less from where we left off last time.
Questions and Answers on Chrys`s Approach to Active Client Pages - Can you access Values and Contents in the Parent Window? (Page 3 of 5 )
Yes. All you need is a reference to the parent window. The DOM window object has a property called “opener.” This is used to return a reference to the parent window. Consider the following statement:
var myParent = window.opener;
The phrase “window.opener” in the statement returns the reference to the immediate parent window. This reference is assigned to the “myParent” variable.
How do I set or read JavaScript variable in the Parent Window?
Assume that in the parent window you have the following JavaScript code:
<script type="text/javascript">
var lastName;
//the rest of the JavaScript code …
</script>
From the JavaScript of the child window, you can set this parent variable giving the value “Smith” as follows:
myParent = window.opener;
myParent.lastName = "Smith";
So, in the JavaScript in the child, you use the reference of the parent window; this is followed by a dot, and then the variable in the parent window. The value (Smith) for the variable is assigned with the equal sign.
The following code in the JavaScript of the child window will read the value of the variable of the parent window and assign it to the name variable in the child.
myParent = window.opener;
var name = myParent.lastName;
How do I set or read HTML Element Content in the Parent Window?
In the JavaScript of the child window the following statement would access the value of an element in the parent window:
myParent = window.opener;
myParent.document.getElementById('I1').value
Assume that you have the following element in the parent window:
<input type=”text” id=”I1”>
The following statement in the child script (window) would set the value of the Input Text Control to “I love you.”
myParent.document.getElementById('I1').value = "I love you.”;
And the following code in the child script would read the value of the Input Text control from the parent window into the variable, name, in the child window:
var name = myParent.document.getElementById('I1').value;