Why does the following throw an “Object doesn'

2019-07-20 03:47发布

问题:

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

回答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.