Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Cannot appendChild() to a newly opened window

Reply
Thread Tools

Cannot appendChild() to a newly opened window

 
 
Michael Lee
Guest
Posts: n/a
 
      07-02-2004
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

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      07-02-2004


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/

 
Reply With Quote
 
 
 
 
Myron Turner
Guest
Posts: n/a
 
      07-03-2004
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 <(E-Mail Removed)>
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accidentaly opened I-Bagle - and then opened virus vault ?? Morph Computer Information 2 02-01-2005 03:43 AM
DOM with newly opened window Christopher Benson-Manica Javascript 2 01-27-2005 01:14 PM
Is that possible I can assign an window object variable to an already opened window datactrl Javascript 3 05-29-2004 06:52 AM
Invalid session when a new IE window is opened using window.open() raj ASP General 8 04-09-2004 06:50 AM
How to get values back from new window opened using window.open() Manjit Javascript 0 07-25-2003 01:40 AM



Advertisments