Calling function residing on Ancestor page from child page

Discussion in 'Javascript' started by M B HONG 20, Dec 13, 2005.

  1. M B HONG 20

    M B HONG 20 Guest

    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.
     
    M B HONG 20, Dec 13, 2005
    #1
    1. Advertising

  2. 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
     
    Thomas 'PointedEars' Lahn, Dec 13, 2005
    #2
    1. Advertising

  3. 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
     
    Thomas 'PointedEars' Lahn, Dec 13, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?c2hla2hhcnRncw==?=

    How to debug an ASP.NET web application residing on a remote serve

    =?Utf-8?B?c2hla2hhcnRncw==?=, Nov 15, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    358
    Cowboy \(Gregory A. Beamer\)
    Nov 15, 2005
  2. Johnny
    Replies:
    0
    Views:
    902
    Johnny
    Oct 2, 2003
  3. Jeff Rodriguez
    Replies:
    23
    Views:
    1,141
    David Schwartz
    Dec 9, 2003
  4. Replies:
    4
    Views:
    322
    Mark Space
    Jun 2, 2008
  5. Tom Ten Thij
    Replies:
    2
    Views:
    115
    Tom Ten Thij
    Feb 23, 2010
Loading...

Share This Page