Cannot appendChild() to a newly opened window

M

Michael Lee

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
 
M

Martin Honnen

Michael said:
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.
 
M

Myron Turner

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;
}



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.

Myron Turner
www.room535.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top