Cannot appendChild() to a newly opened window

Discussion in 'Javascript' started by Michael Lee, Jul 2, 2004.

  1. Michael Lee

    Michael Lee Guest

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

    function ShowTable()
    {
    clonedNode = document.getElementById("myTable").cloneNode(true);
    win = window.open();
    win.document.body.appendChild(clonedNode);
    }

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

    Thanks!

    Michael Lee
    Michael Lee, Jul 2, 2004
    #1
    1. Advertising

  2. Michael Lee wrote:

    > Does anyone know why the following function works in FireFox but not in IE6?
    >
    > function ShowTable()
    > {
    > clonedNode = document.getElementById("myTable").cloneNode(true);
    > win = window.open();
    > win.document.body.appendChild(clonedNode);
    > }


    First window.open() doesn't load any document, and even if you used
    window.open('whatever.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.body
    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
    insertAdjacentHTML.
    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/
    Martin Honnen, Jul 2, 2004
    #2
    1. Advertising

  3. Michael Lee

    Myron Turner Guest

    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:
    http://www.room535.org/news/
    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.getNodesFromWindow_1(node_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 cloneExternalNode(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
    getNodesFromWindow_1(nodes) {
    for(var i=0; i < nodes.length; i++) {
    // pass parent, if exits or else null
    var clone = cloneExternalNode(n,parent);
    mainDiv.appendChild(clone);
    }
    }

    function cloneExternalNode(n,parent)
    {
    if(n.nodeType == 3) {
    var newNode = document.createTextNode(n.data);
    }
    else {
    var newNode = document.createElement(n.nodeName);
    var att = n.attributes;
    setcloneAttributes(newNode, att);
    }
    if(parent) parent.appendChild(newNode);

    var children = n.childNodes;
    for(var j=0; j<children.length; j++) {
    cloneExternalNode(children[j], newNode);
    }

    return newNode;
    }



    On Fri, 02 Jul 2004 19:49:25 +0200, Martin Honnen <>
    wrote:

    >
    >
    >Michael Lee wrote:
    >
    >> Does anyone know why the following function works in FireFox but not in IE6?
    >>
    >> function ShowTable()
    >> {
    >> clonedNode = document.getElementById("myTable").cloneNode(true);
    >> win = window.open();
    >> win.document.body.appendChild(clonedNode);
    >> }

    >
    >First window.open() doesn't load any document, and even if you used
    > window.open('whatever.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.body
    >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
    >insertAdjacentHTML.
    >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/
    >


    Myron Turner
    www.room535.org
    Myron Turner, Jul 3, 2004
    #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. raj
    Replies:
    8
    Views:
    531
    J. Baute
    Apr 9, 2004
  2. Manjit
    Replies:
    0
    Views:
    207
    Manjit
    Jul 25, 2003
  3. datactrl
    Replies:
    3
    Views:
    128
    Thomas 'PointedEars' Lahn
    May 29, 2004
  4. Christopher Benson-Manica

    DOM with newly opened window

    Christopher Benson-Manica, Jan 26, 2005, in forum: Javascript
    Replies:
    2
    Views:
    112
    Martin Honnen
    Jan 27, 2005
  5. Mark
    Replies:
    2
    Views:
    122
Loading...

Share This Page