Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calling function in a child window from main parent window

8 views
Skip to first unread message

Bhishm

unread,
Sep 3, 2007, 7:45:19 AM9/3/07
to
Hi,

I am opening a child window from parent using window.open, but on
refreshing the parent the reference of the child is lost.

Is there any way to save the reference of the child in the parent
after refresh.

I have searched a lot but not able to find any relevant information

Thanks & Regards,
Bhishm

Erwin Moller

unread,
Sep 3, 2007, 8:39:49 AM9/3/07
to

Hi,

If the parent is refreshed, so are all the javascript references.
The good news is that the CHILD can find the parent by the using
parent-keyword.

If that does not help you, you could try to find the childwindow by
using its name (assuming you don't change that).

Regards,
Erwin Moller

Thomas 'PointedEars' Lahn

unread,
Sep 3, 2007, 7:08:13 PM9/3/07
to
Bhishm wrote:
> I am opening a child window from parent using window.open, but on
> refreshing the parent the reference of the child is lost.

You should get your terminology right. There is no intrinsic parent-child
relationship a window and another that was opened this way. To avoid
confusion with frames, where there is a parent-child relationship, it is
best to say "the opening window/the opener" and "the opened window", or to
use identifiers like A and B, such as in "window B is opened from window A".

> Is there any way to save the reference of the child in the parent
> after refresh.

There is no way that the opener (the Window object to retain the global
execution context from which window.open() was called) can retain a
reference to the opened Window when the former's document is refreshed.
When that happens, the execution context where a corresponding property was
set ceases to exist (at least one should not rely on its fully continued
existence; there are only browser bugs that allow it to "survive" sometimes).

However, an (X)HTML document (in a window) can contain frames, and a frame
also is represented by a Window object in DOM Level 0 compliant DOMs (i.e.
all of the DOMs I have encountered to date). So you could save the
reference to the opened Window in a property defined in the execution
context of another frame -- with all the accessibility issues frames imply.
Say, a frameset contains two frames on the same level named "frameA" and
"frameB", and that you want to save a value in frameB from frameA:

window.parent.frames["frameB"].foo = "bar";

That is relying on frame support, and that the global execution context of a
window can be referred to by a reference to the Window object that creates
that context. (It would then be wise to have `foo' declared globally in
"frameB", to avoid implications with the host object pecularities allowed by
ECMA-262.)

But the opened window has a reference to its opener with window.opener in
*its* execution contexts; the Same Origin Policy obeyed, the opened window
can regularly be checked for whether its document was fully loaded (using
window.setTimeout() or window.setInterval()) and then the reference to the
opened window can be saved again in the global execution context of the opener:

function checkOpener(t)
{
if (window.opener && !window.opener.closed
&& window.opener.document && window.opener.document.body)
{
window.clearTimeout(timeout);
window.opener.foo = "bar";
}
else
{
timeout = window.setTimeout("checkOpener(" + t + ")", t);
}
}

var t = 500;
var timeout = window.setTimeout("checkOpener(" + t + ")", t);

Another possibility includes accessing the opened window by name repeatedly,
however that would cause its document to reload:

window.open("foo.html", "windowB", "...");

The usual feature tests should be applied in all cases.


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann

0 new messages