Search code examples
javascripthtmlappendnew-window

Appending same node in different windows


I want to append an object, that was created in a parent window to a child window:

div = document.createElement( "div" );
document.body.appendChild( div );
// Here come div's atts;
render = window.open().document;
render.body.appendChild( div );

but the new DIV is appended only to the child window. If I comment the last line - the div is appended to the parent window. Can that be solved?


Solution

  • Editing since I misread the question:

    newelement = element.cloneNode(true); // true to clone children too
    

    Still there is no html or body in the new window that it can be appended to. At least not in chrome.

    Try this instead:

    <html>
    <body>
        <script>
            div = document.createElement( "div" );
            // add some text to know if it's really there
            div.innerText = 'text of the div';
            document.body.appendChild( div );
            // Here come div's atts;
            render = window.open().document;
            // create the body of the new document
            render.write('<html><body></body></html>');
            // now you can append the div
            render.body.appendChild( div );
            alert(render.body.innerHTML);
        </script>
    </body>
    </html>