DOM with newly opened window

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    #1

    DOM with newly opened window

    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,scr ollbars=0' );
    var fragment=aWin.d ocument.createD ocumentFragment ();
    var head=fragment.a ppendChild( document.create Element('head') );
    var e=head.appendCh ild( document.create Element('script ') );
    e.text="alert(' hello, world!')";
    aWin.document.d ocumentElement. 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.append Child 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)cybers pace.org | don't, I need to know. Flames welcome.
  • Christopher Benson-Manica

    #2
    Re: DOM with newly opened window

    Christopher Benson-Manica <ataru@nospam.c yberspace.org> spoke thus:
    [color=blue]
    > var fragment=aWin.d ocument.createD ocumentFragment ();
    > var head=fragment.a ppendChild( document.create Element('head') );
    > var e=head.appendCh ild( document.create Element('script ') );[/color]

    So it turns out that I need aWin.document.c reateElement 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)cybers pace.org | don't, I need to know. Flames welcome.

    Comment

    • Martin Honnen

      #3
      Re: DOM with newly opened window



      Christopher Benson-Manica wrote:

      [color=blue]
      > So it turns out that I need aWin.document.c reateElement 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?[/color]

      The W3C DOM standard in Level 2 has defined a method
      var importedNode = document.import Node(nodeFromOt herDocument,
      true/false);
      // now insert e.g.
      document.docume ntElement.appen dChild(imported Node);
      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/

      Comment

      Working...