By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,084 Members | 1,261 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,084 IT Pros & Developers. It's quick & easy.

Calling function residing on Ancestor page from child page

P: n/a
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.

Dec 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
M B HONG 20 wrote:
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
Dec 13 '05 #2

P: n/a
Thomas 'PointedEars' Lahn wrote:
M B HONG 20 wrote:
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();
}


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
Dec 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.