PopupWindow.document.getElementById() doesn't work for Mozilla?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nick

    #1

    PopupWindow.document.getElementById() doesn't work for Mozilla?

    I have the following code:
    var w=window.open(' popup.htm','',' width=400,heigh t=400');

    w.opener = this;

    var img = w.document.getE lementById('pic ');

    However, the img variable is always null in Mozilla Firefox. But it works
    well in IE.

    What's teh proper way to access DOM in the popuped window?


  • Michael Winter

    #2
    Re: PopupWindow.doc ument.getElemen tById() doesn't work for Mozilla?

    On Thu, 30 Sep 2004 14:27:12 -0400, nick <nbdy9.removeth is@hotmail.com>
    wrote:
    [color=blue]
    > I have the following code:
    > var w=window.open(' popup.htm','',' width=400,heigh t=400');
    >
    > w.opener = this;[/color]

    That is unnecessary. The opener property of the new window will refer to
    the opening window automatically.
    [color=blue]
    > var img = w.document.getE lementById('pic ');
    >
    > However, the img variable is always null in Mozilla Firefox. But it
    > works well in IE.[/color]

    Have you named the IMG element (that is, name="pic") or given it an id?
    Are you sure that the page has loaded before executing the line above?
    [color=blue]
    > What's teh proper way to access DOM in the popuped window?[/color]

    winRef.document .images['image-name-or-id']

    is the best way to access images as it will be supported by both old and
    new browsers.

    Hope that helps,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • DU

      #3
      Re: PopupWindow.doc ument.getElemen tById() doesn't work for Mozilla?

      nick wrote:
      [color=blue]
      > I have the following code:
      > var w=window.open(' popup.htm','',' width=400,heigh t=400');
      >
      > w.opener = this;
      >[/color]

      Useless and wrong. Avoid coding like that.
      [color=blue]
      > var img = w.document.getE lementById('pic ');
      >[/color]

      Right here, you're assuming a synchronous chain of event: the window
      might be created but the document might still be loading. So you're
      referencing the document which has not been loaded yet.
      [color=blue]
      > However, the img variable is always null in Mozilla Firefox.[/color]

      Because the document still has not fired the load event and created
      pointers in the memory heap.

      But it works[color=blue]
      > well in IE.
      >
      > What's teh proper way to access DOM in the popuped window?
      >
      >[/color]

      Get an onload event in the body of the popup window to call a function
      in the opener to access the picture. It works reliably in NS 7.x,
      Mozilla 1.x, Firefox 0.8+, K-meleon 0.8+, Opera 7.x, MSIE 6 SP2.

      DU
      --
      The site said to use Internet Explorer 5 or better... so I switched to
      Mozilla 1.7.3 :)

      Comment

      Working...