Opening a new window and attaching an onload event listener.

Discussion in 'Javascript' started by Daz, Dec 30, 2006.

  1. Daz

    Daz Guest

    Hello everyone,

    I would like to open a child window from the parent, and add an onload
    event listener to the child window which will tell the parent when the
    document has loaded. As far as I know, this shouldn't be an issue, but
    I just can't get it to work. The script only needs to work with
    Firefox/Mozilla, so XP code isn't an issue.

    I have tried to open a window like so.

    var newWindow = open('','','');

    This works as expected.
    Next I redirect the child window:

    newWindow.location.href = 'http://www.google.co.uk';

    Again, this works no problem.
    before the child window is redirected, I have tried several methods of
    adding an onload event listener to the window, from the script inside
    the parent window, but nothing seems to work. I have tried:

    newWindow.onload = windowLoaded;
    newWindow.document.onload = windowLoaded;
    newWindow.addEventListener('onload', windowLoaded, true);
    newWindow.document.addEventListener('onload', windowLoaded, true);

    None of which seem to work. Please could someone explain where I have
    screwed up?

    Many thanks.

    Daz.
     
    Daz, Dec 30, 2006
    #1
    1. Advertising

  2. Daz

    Fred Guest

    Daz wrote:
    > Hello everyone,
    >
    > I would like to open a child window from the parent, and add an onload
    > event listener to the child window which will tell the parent when the
    > document has loaded. As far as I know, this shouldn't be an issue, but
    > I just can't get it to work. The script only needs to work with
    > Firefox/Mozilla, so XP code isn't an issue.
    >
    > I have tried to open a window like so.
    >
    > var newWindow = open('','','');
    >
    > This works as expected.
    > Next I redirect the child window:
    >
    > newWindow.location.href = 'http://www.google.co.uk';
    >
    > Again, this works no problem.
    > before the child window is redirected, I have tried several methods of
    > adding an onload event listener to the window, from the script inside
    > the parent window, but nothing seems to work. I have tried:
    >
    > newWindow.onload = windowLoaded;
    > newWindow.document.onload = windowLoaded;
    > newWindow.addEventListener('onload', windowLoaded, true);
    > newWindow.document.addEventListener('onload', windowLoaded, true);
    >
    > None of which seem to work. Please could someone explain where I have
    > screwed up?


    When you open your popup, it contains an empty document. You attach an
    onload handler to the empty document, then re-direct the URL so that
    document is destroyed and a new one is loaded.

    You have to put the onload handler in the new document, you can't
    attach it from the calling page unless you actually write the document
    from the calling page (say using document.write).


    --
    Fred.
     
    Fred, Dec 30, 2006
    #2
    1. Advertising

  3. Daz

    Daz Guest

    Fred wrote:

    > Daz wrote:
    > > Hello everyone,
    > >
    > > I would like to open a child window from the parent, and add an onload
    > > event listener to the child window which will tell the parent when the
    > > document has loaded. As far as I know, this shouldn't be an issue, but
    > > I just can't get it to work. The script only needs to work with
    > > Firefox/Mozilla, so XP code isn't an issue.
    > >
    > > I have tried to open a window like so.
    > >
    > > var newWindow = open('','','');
    > >
    > > This works as expected.
    > > Next I redirect the child window:
    > >
    > > newWindow.location.href = 'http://www.google.co.uk';
    > >
    > > Again, this works no problem.
    > > before the child window is redirected, I have tried several methods of
    > > adding an onload event listener to the window, from the script inside
    > > the parent window, but nothing seems to work. I have tried:
    > >
    > > newWindow.onload = windowLoaded;
    > > newWindow.document.onload = windowLoaded;
    > > newWindow.addEventListener('onload', windowLoaded, true);
    > > newWindow.document.addEventListener('onload', windowLoaded, true);
    > >
    > > None of which seem to work. Please could someone explain where I have
    > > screwed up?

    >
    > When you open your popup, it contains an empty document. You attach an
    > onload handler to the empty document, then re-direct the URL so that
    > document is destroyed and a new one is loaded.
    >
    > You have to put the onload handler in the new document, you can't
    > attach it from the calling page unless you actually write the document
    > from the calling page (say using document.write).
    >
    >
    > --
    > Fred.


    So basically, I need to initiate the page load, and then attach the
    event listener? I'm not sure if that will work. I could right, say a
    carriage return to the child window, but as soon as I change the
    location of the page, I will no doubt lose the event handler.

    What I don't understand, is why I can't attach it to the window, as
    opposed to the document.

    What I am trying to achieve, is an application which can run from the
    parent window, and use the child window to navigate to various pages on
    the site, and get information from each page (such as links, and user
    specific information), so it will effectively work as a macro, saving
    the user time waiting for pages to load and clicking on more links. I
    am not sure the best way to go about doing this, and the best way I
    could think of (above), clearly isn't going to work...

    Thank you for your comments Fred. :)
     
    Daz, Dec 31, 2006
    #3
  4. Daz

    Daz Guest

    Jim Land (NO SPAM) wrote:

    > "Daz" <> wrote in news:1167542962.890494.88000
    > @i12g2000cwa.googlegroups.com:
    >
    > > ...attach the event listener...
    > > What I don't understand, is why I can't attach it to the window, as
    > > opposed to the document.
    > >

    >
    > Oh, you can attach it to the window, all right, but as soon as you direct
    > the window to a new URL, what happens? The window discards your attachment
    > and loads a whole new page.


    Oh I see. I keep getting confused between document, and window. I
    thought it was the _document_ being redirected, not the window. Hehe.

    Thanks for clearing that up for me Jim. :)
     
    Daz, Dec 31, 2006
    #4
  5. Daz

    Guest

    Hi.
    I've been struggling with the very same issue for the past few days. I
    want to be able to load a new document from a parent window and read
    some parameters from the newly opened document. I too, tried attaching
    an onload event to the new document, but got undefined values when
    trying to read values from the new document. I couldn't find-out why,
    until this thread gave me the answer. So, thanks. But, still, I didn't
    achieve my goal yet, is there a way it can be done?
    Thanks,
    - Yuval

    Daz כתב:
    > Jim Land (NO SPAM) wrote:
    >
    > > "Daz" <> wrote in news:1167542962.890494.88000
    > > @i12g2000cwa.googlegroups.com:
    > >
    > > > ...attach the event listener...
    > > > What I don't understand, is why I can't attach it to the window, as
    > > > opposed to the document.
    > > >

    > >
    > > Oh, you can attach it to the window, all right, but as soon as you direct
    > > the window to a new URL, what happens? The window discards your attachment
    > > and loads a whole new page.

    >
    > Oh I see. I keep getting confused between document, and window. I
    > thought it was the _document_ being redirected, not the window. Hehe.
    >
    > Thanks for clearing that up for me Jim. :)
     
    , Feb 9, 2007
    #5
  6. Daz

    Daz Guest

    On Feb 9, 5:16 am, wrote:
    > Hi.
    > I've been struggling with the very same issue for the past few days. I
    > want to be able to load a new document from a parent window and read
    > some parameters from the newly opened document. I too, tried attaching
    > an onload event to the new document, but got undefined values when
    > trying to read values from the new document. I couldn't find-out why,
    > until this thread gave me the answer. So, thanks. But, still, I didn't
    > achieve my goal yet, is there a way it can be done?
    > Thanks,
    > - Yuval
    >
    > Daz כתב:
    >
    > > Jim Land (NO SPAM) wrote:

    >
    > > > "Daz" <> wrote in news:1167542962.890494.88000
    > > > @i12g2000cwa.googlegroups.com:

    >
    > > > > ...attach the event listener...
    > > > > What I don't understand, is why I can't attach it to the window, as
    > > > > opposed to the document.

    >
    > > > Oh, you can attach it to the window, all right, but as soon as you direct
    > > > the window to a new URL, what happens? The window discards your attachment
    > > > and loads a whole new page.

    >
    > > Oh I see. I keep getting confused between document, and window. I
    > > thought it was the _document_ being redirected, not the window. Hehe.

    >
    > > Thanks for clearing that up for me Jim. :)


    I think the only solution would be to have a small function in the
    child window that executes a function in the parent window onload. You
    also need to ensure that the child window is served from the same
    domain as the parent window.
     
    Daz, Feb 10, 2007
    #6
    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. Per
    Replies:
    1
    Views:
    366
    Howard
    Apr 18, 2005
  2. David Otton

    window.onload and body.onload differences

    David Otton, Nov 4, 2004, in forum: Javascript
    Replies:
    2
    Views:
    574
    Martin Honnen
    Nov 4, 2004
  3. keith
    Replies:
    2
    Views:
    326
  4. Replies:
    5
    Views:
    277
    Thomas 'PointedEars' Lahn
    May 15, 2005
  5. Mike Scirocco
    Replies:
    4
    Views:
    276
    Mike Scirocco
    Apr 5, 2007
Loading...

Share This Page