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 I make my remarks now? (Page 4 of 5 )
Yes. We have a dialog here:
Reader: With the document phase, the parent document can only access the content of the HTML elements in the child document. With the document phase, the parent document cannot access the JavaScript variables, JavaScript arrays and JavaScript Object properties in the child document.
However, with the window phase, the parent window can access JavaScript variables, JavaScript arrays, and JavaScript Object properties, including HTML contents in the child document. With the window phase the child window can also access JavaScript variables, JavaScript arrays, and JavaScript Object properties, including HTML contents in the parent element. Here the main thing we need is reference to the child window or parent window.
Author: Your remarks are correct. We shall make the document phase a component of the window phase.
How do you get the reference of a different window?
The question is how do you get the reference of an ancestor or descendant window. There is no straightforward method for doing this. You need a process. Let us first look at how to get the reference of an ancestor window.
Getting the reference of an ancestor window
This is my way of solving the problem. You write a particular code in each of the windows. This the code:
function precedeOrReturn(name)
{
if (window.name == name)
{
return window.self;
}
else
{
parentWin = window.opener;
return parentWin.precedeOrReturn(name);
}
}
Each window can call the function that is in its JavaScript. When you call the function, you send the name of the ancestor window you are interested in as an argument. The function first checks if the name sent is the name of its window (where the function resides). If that is the case, it develops a reference to its window and returns it to the calling function with the following statement:
return window.self;
If the name is not the name of its window, then it calls the same function in the immediate parent with the following statement:
parentWin = window.opener;
return parentWin.precedeOrReturn();
I call the above code the Backward Propagation Code.
To execute the above series of functions from any descendant window, you can use the following statement:
theRef = precedeOrReturn('ancestorName')
The reference returned is assigned to theRef. The call will be sent from window to window until the window with the ancestor name is reached. Then the reference is returned; it moves automatically in the reverse direction, from window to window, until the theRef variable gets it.
You can now use this reference to access values and JavaScript functions in the ancestor window, just as we did for the child and parent window above.
Getting the reference of a descendant window
You do the opposite of the above. You can put the following code in the JavaScript in every window:
function followOrReturn(name)
{
if (window.name == name)
{
return window.self;
}
else
{
return window2.followOrReturn(name);
}
}
This function, like the one before, is called by another function or event in the same window. The function receives the name of the descendant window that interests you as an argument. It first checks if the name it has received is the name of its window. If that is true, it determines the reference of the window it is in and returns it; otherwise, it forwards the request to the immediate child window calling the same function there. I call the above code the Forward Propagation Code.
In order to call a function in the child window, it needs a reference to the child window. We have been opening child windows as follows:
childReference = window.open();
where “window ” is the window having the JavaScript code. As you can see, each time we do this, we have a reference to the child window in the parent window. From the parent window you can use this reference to do something in the child window.
The following statement in your current page will get the reference of some descendant window:
theRef = followOrReturn(descendantName);
In this and in the above case, the request is propagated. The result is also propagated.
Can propagation occur through a closed window?
No. If you have a series of windows opened, and you close any within the ends of the windows, propagation would not go through it. It is advisable to close windows in this order: the most descendant is closed first, the second most descendant is closed next, and so on.