检查文档是否为ROOT节点

Van*_*ing 6 javascript

我需要知道文档元素是否是页面的ROOT节点.例如:

<html> <-- ROOT Node
   <head></head>
   <body>
      <iframe>
         <html>...</html> <-- other document 
      </iframe>
      <iframe>
         <html>...</html> <-- other document
      </iframe>
   </body>
</html>
Run Code Online (Sandbox Code Playgroud)

在iframe 1或2中执行的Javascript应该知道它们的文档节点是否是根节点.

希望您能够帮助我.

lon*_*day 12

你应该可以这样做top:

if (window.top.document === window.document) {
    // we're in the outermost window
}
Run Code Online (Sandbox Code Playgroud)