DOM with newly opened window

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

I'm trying to use the DOM to write in a popped up window, like so:

<html>
<head>
<title>Test</title>
</head>
<body>
<script type="text/javascript">
var aWin=open( '', '_blank', 'width='+640+',height='+480+', resizable=0,scrollbars=0' );
var fragment=aWin.document.createDocumentFragment();
var head=fragment.appendChild( document.createElement('head') );
var e=head.appendChild( document.createElement('script') );
e.text="alert('hello, world!')";
aWin.document.documentElement.appendChild( fragment );
</script></body></html>

Netscape 6 and FireFox seem to handle it just fine, but Opera does
nothing and IE6 complains about 'no such interface' on the
fragment.appendChild line (or crashes outright!). I presume I'm doing
something wrong, but I have no idea what... Any help would be
appreciated.
 
C

Christopher Benson-Manica

Christopher Benson-Manica said:
var fragment=aWin.document.createDocumentFragment();
var head=fragment.appendChild( document.createElement('head') );
var e=head.appendChild( document.createElement('script') );

So it turns out that I need aWin.document.createElement for this to
work. I'm curious, though - why doesn't IE let you add an element
you've created with one document to another document's tree? Is that
standard behavior, or is it a cruel M$ trick?
 
M

Martin Honnen

Christopher Benson-Manica wrote:

So it turns out that I need aWin.document.createElement for this to
work. I'm curious, though - why doesn't IE let you add an element
you've created with one document to another document's tree? Is that
standard behavior, or is it a cruel M$ trick?

The W3C DOM standard in Level 2 has defined a method
var importedNode = document.importNode(nodeFromOtherDocument,
true/false);
// now insert e.g.
document.documentElement.appendChild(importedNode);
as the proper way to be able to prepare to insert a node from one
document into the other so the W3C DOM indeed expects that
implementations do not necessarily allow you to move nodes from one
document to the other.
Of course all IE DOM stuff is mostly grown before W3C DOM Level 2 became
a recommendation, it doesn't have importNode for instance even in IE 6.
Indeed if one looks closely at the W3C DOM Level 2 Core methods like
appendChild in
<http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1950641247>
then it says:
Exceptions: "WRONG_DOCUMENT_ERR: Raised if newChild was created from
a different document than the one that created this node."
so an implementation is even required to throw an exception if a node
from one document is inserted in another document.
I think Mozilla doesn't, Opera 7 for instance does.
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top