2

Why does the following throw an "Object doesn't support property or method 'importNode'" in IE11?

Could it be my "document mode"? I am in "document mode" 7.

    <!DOCTYPE html>
    <html>
    <head>
        <script>
            function go() {
              var popUp = window.open('about:blank');
              var node = document.createElement('div');
              node.textContent = 'foo';

              var importedNode = popUp.document.importNode(node, true);
              popUp.document.body.appendChild(importedNode);
            }
        </script>
    </head>
    <body>
      <button onclick="go()">Click Me</button>
    </body>
    </html>

For clarification I want the node, node to be created by the opener window, and I am using importNode in order to attempt to get this working in IE (it is not needed for Chrome).

importNode was added in IE9 I think (https://msdn.microsoft.com/en-us/library/ie/gg130964%28v=vs.85%29.aspx).

Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • `about:blank` doesn't have DTD, hence `popUp` falls to the quirks mode? And you definitely should be in IE9 mode to use features introduced in IE9 ... – Teemu Feb 24 '15 at 19:41
  • 1
    I don't think IE supports `importNode`, even though it is there. I get `No such interface supported` in IE 11, also http://stackoverflow.com/questions/1811116/ie-support-for-dom-importnode – Musa Feb 24 '15 at 19:48
  • @Teemu Do document modes correspond to browser version numbers? If so, why is IE falling into mode 7 given the above code/markup. – Ben Aston Feb 24 '15 at 20:00
  • Pretty much yes, except the last version which can be referred with "edge" too. Just omit any `x-ua` meta tag and use proper html5 DTD, that would be the best choice to get IE9 to work even near like a modern browser. If a compatibility mode is activated automatically, please [check this answer](http://stackoverflow.com/a/13287226/1169519). – Teemu Feb 24 '15 at 20:03
  • But the above has a valid doctype right? Also, can I inject a doctype into about:blank successfully to avoid quirks mode in the target? – Ben Aston Feb 24 '15 at 20:04
  • About the code injection, afaik `document.write()` is the only way, unless a real page will be loaded. – Teemu Feb 24 '15 at 20:11
  • Are you loading the page locally from your hard drive? If so, it's likely being displayed in the Intranet zone and therefore defaulting to IE7 compatibility mode. You can a) add an MOTW to load your page from the Internet zone (https://msdn.microsoft.com/en-us/library/ms537628(v=vs.85).aspx), b) serve it from a local web server, c) disable the settings that automatically set Intranet zone to IE7 mode. (See http://stackoverflow.com/questions/27272454/ie11-document-mode-defaults-to-ie7-how-to-reset/27278277#27278277). And, yes, you must be in IE9 standards mode or later to use importNode. – Lance Leonard Feb 24 '15 at 21:14
  • @LanceLeonard OK thanks that sounds like it. I'll accept if you make this the answer. Thanks – Ben Aston Feb 24 '15 at 21:27

1 Answers1

1

Are you loading the page locally from your hard drive? If so, it's likely being displayed in the Intranet zone and therefore defaulting to IE7 compatibility mode. You can:

  1. add an MOTW to load your page from the Internet zone,
  2. serve it from a local web server,
  3. disable the settings that automatically set Intranet zone pages to compatibility view (aka IE7 mode).

And, yes, you must be in IE9 standards mode or later to use importNode.

Hope this helps...

-- Lance

P.S. Coverted to answer, per OP.

Community
  • 1
  • 1
Lance Leonard
  • 3,285
  • 3
  • 16
  • 15