DOM with newly opened window

Discussion in 'Javascript' started by Christopher Benson-Manica, Jan 26, 2005.

  1. 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.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Jan 26, 2005
    #1
    1. Advertising

  2. Christopher Benson-Manica <> spoke thus:

    > 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?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Jan 26, 2005
    #2
    1. Advertising

  3. 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.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jan 27, 2005
    #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:
    560
    J. Baute
    Apr 9, 2004
  2. Manjit
    Replies:
    0
    Views:
    220
    Manjit
    Jul 25, 2003
  3. datactrl
    Replies:
    3
    Views:
    140
    Thomas 'PointedEars' Lahn
    May 29, 2004
  4. Michael Lee

    Cannot appendChild() to a newly opened window

    Michael Lee, Jul 2, 2004, in forum: Javascript
    Replies:
    2
    Views:
    110
    Myron Turner
    Jul 3, 2004
  5. Mark
    Replies:
    2
    Views:
    126
Loading...

Share This Page