Cannot appendChild() to a newly opened window

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

    #1

    Cannot appendChild() to a newly opened window

    Does anyone know why the following function works in FireFox but not in IE6?

    function ShowTable()
    {
    clonedNode = document.getEle mentById("myTab le").cloneNode( true);
    win = window.open();
    win.document.bo dy.appendChild( clonedNode);
    }

    I am trying to display a table with id="myTable" in a new window. IE6
    complains that "document.b ody" is null or not an object.

    Thanks!

    Michael Lee

  • Martin Honnen

    #2
    Re: Cannot appendChild() to a newly opened window



    Michael Lee wrote:
    [color=blue]
    > Does anyone know why the following function works in FireFox but not in IE6?
    >
    > function ShowTable()
    > {
    > clonedNode = document.getEle mentById("myTab le").cloneNode( true);
    > win = window.open();
    > win.document.bo dy.appendChild( clonedNode);
    > }[/color]

    First window.open() doesn't load any document, and even if you used
    window.open('wh atever.html')
    the loading of the document would happen asynchronously and the next
    line is executed without waiting for the document to load so directly
    accessing
    win.document.bo dy
    is not going to work reliably and consistently.
    Furthermore IE doesn't allow you to take nodes from one document and
    insert them into another document. Nor does Opera. The W3C DOM suggests
    to use importNode and Opera and Mozilla implement that. For IE you are
    left with implementing that yourself or using innerHTML/outerHTML and/or
    insertAdjacentH TML.
    The solution to your window access is probably to load a page with
    window.open that in its onload handler copies the table from the opener
    window's document.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Myron Turner

      #3
      Re: Cannot appendChild() to a newly opened window

      While you may not be able to direclty append a node from one wndow to
      the other, there is a work-around, which I've used in:

      You can transfer a reference (or array of references) to the nodes in
      window 1 to window 2, as follows.

      ------------------------------------------
      In Window 1:

      var toWin = top.opener;
      toWin.getNodesF romWindow_1(nod e_arry);

      ------------------------------------------------------------

      In Window 2:
      What you do in Window 2 will depend on what you want to do with the
      node, but this is a stripped down idea of what's involved. The
      function cloneExternalNo de(n,parent) is key to the transfer; it
      creates a new node from the node reference obtained from Window 1 and
      then you can do what you want with it.

      var mainDiv; //needs to be defined
      getNodesFromWin dow_1(nodes) {
      for(var i=0; i < nodes.length; i++) {
      // pass parent, if exits or else null
      var clone = cloneExternalNo de(n,parent);
      mainDiv.appendC hild(clone);
      }
      }

      function cloneExternalNo de(n,parent)
      {
      if(n.nodeType == 3) {
      var newNode = document.create TextNode(n.data );
      }
      else {
      var newNode = document.create Element(n.nodeN ame);
      var att = n.attributes;
      setcloneAttribu tes(newNode, att);
      }
      if(parent) parent.appendCh ild(newNode);

      var children = n.childNodes;
      for(var j=0; j<children.leng th; j++) {
      cloneExternalNo de(children[j], newNode);
      }

      return newNode;
      }



      On Fri, 02 Jul 2004 19:49:25 +0200, Martin Honnen <mahotrash@yaho o.de>
      wrote:
      [color=blue]
      >
      >
      >Michael Lee wrote:
      >[color=green]
      >> Does anyone know why the following function works in FireFox but not in IE6?
      >>
      >> function ShowTable()
      >> {
      >> clonedNode = document.getEle mentById("myTab le").cloneNode( true);
      >> win = window.open();
      >> win.document.bo dy.appendChild( clonedNode);
      >> }[/color]
      >
      >First window.open() doesn't load any document, and even if you used
      > window.open('wh atever.html')
      >the loading of the document would happen asynchronously and the next
      >line is executed without waiting for the document to load so directly
      >accessing
      > win.document.bo dy
      >is not going to work reliably and consistently.
      >Furthermore IE doesn't allow you to take nodes from one document and
      >insert them into another document. Nor does Opera. The W3C DOM suggests
      >to use importNode and Opera and Mozilla implement that. For IE you are
      >left with implementing that yourself or using innerHTML/outerHTML and/or
      >insertAdjacent HTML.
      >The solution to your window access is probably to load a page with
      >window.open that in its onload handler copies the table from the opener
      >window's document.
      >
      >
      >--
      >
      > Martin Honnen
      > http://JavaScript.FAQTs.com/
      >[/color]

      Myron Turner

      Comment

      Working...