Calling function residing on Ancestor page from child page

M

M B HONG 20

Hi all -

I am working on a web application and have run into a problem. From a
main window (Window1) I open a child window (Window2), and from that
child window I open a grandchild window (Window3). I need to call a
funcion that resides on Window1 from Window3. I do this successfully
by calling a function with window.opener on Window3, which calls a
function with window.opener on Window2, which calls a function on
Window1.
My problem comes in with the case of a user closing Window2, and trying
to carry out the function on Window3 that calls the function on Window
1. I have already tried window.top, but it doesnt seem to work.
window.opener of course returns the error "window.opener has no
properties". Any help would be greatly appreciated. Thanks in advance.
 
T

Thomas 'PointedEars' Lahn

M said:
I am working on a web application and have run into a problem. From a
main window (Window1) I open a child window (Window2), and from that
child window I open a grandchild window (Window3). I need to call a
funcion that resides on Window1 from Window3.

In "Window3"'s script code:

window.opener.opener.functionIdentifier(...)
I do this successfully by calling a function with window.opener on
Window3, which calls a function with window.opener on Window2, which
calls a function on Window1.

Not necessary. If each partial reference resolves to an object, you
should be able to use the referencing above.
My problem comes in with the case of a user closing Window2, and trying
to carry out the function on Window3 that calls the function on Window
1.
Indeed.

I have already tried window.top, but it doesnt seem to work.

And it should not work. The `top' property of Window host objects refers
to the top Window object within the frame hierarchy, i.e. to the base Window
object itself if there are no frames in the window. Each window has its
own frame hierarchy.
window.opener of course returns the error "window.opener has no
properties". Any help would be greatly appreciated. Thanks in advance.

When Window3 is opened from Window2 (reliable), or when Window2 is closed
(unreliable), you have to tell Window3 the reference to Window1.

In Window2:

var w = window.open(..., "Window3", ...);
if (w)
{
w.window1 = window.opener;
}

In Window3:

if (window.window1
&& !window.window1.closed
&& typeof window.window1.functionIdentifier == "function")
{
window.window1.functionIdentifier();
}

(All this is not considering that windows may contain framesets, in which
case you may have to use the `top' property between to retrieve a reference
to the top frameset window.)

Note that your playing around with popup windows is potentially harmful
in the first place (ask Google), so you should reconsider the design of
your Web application.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
When Window3 is opened from Window2 (reliable), or when Window2 is closed
(unreliable), you have to tell Window3 the reference to Window1.

In Window2:

var w = window.open(..., "Window3", ...);
if (w)
{
w.window1 = window.opener;
}

In Window3:

if (window.window1
&& !window.window1.closed
&& typeof window.window1.functionIdentifier == "function")
{
window.window1.functionIdentifier();
}

Forget about that, I have tested it and strangely it does not work
as intended in Firefox, not even with referring directly to the
Global object.

So instead use in Window3, unconditionally,

var o, window1 = ((o = window.opener) && !o.closed && o.opener);

and later

if (window1
&& !window1.closed
&& typeof window1.functionIdentifier == "function")

{
window1.functionIdentifier(...);
}

Should work as intended.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top