最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if document is ROOT node - Stack Overflow

programmeradmin0浏览0评论

I need to know if the document element is the ROOT node of the page. For example:

<html> <-- ROOT Node
   <head></head>
   <body>
      <iframe>
         <html>...</html> <-- other document 
      </iframe>
      <iframe>
         <html>...</html> <-- other document
      </iframe>
   </body>
</html>

Javascript that is executed in iframe 1 or 2 should know if their document node is the root node.

Hope you can help me.

I need to know if the document element is the ROOT node of the page. For example:

<html> <-- ROOT Node
   <head></head>
   <body>
      <iframe>
         <html>...</html> <-- other document 
      </iframe>
      <iframe>
         <html>...</html> <-- other document
      </iframe>
   </body>
</html>

Javascript that is executed in iframe 1 or 2 should know if their document node is the root node.

Hope you can help me.

Share Improve this question asked Feb 4, 2011 at 8:53 Van CodingVan Coding 24.5k25 gold badges92 silver badges137 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 14

You should be able to do this with top:

if (window.top.document === window.document) {
    // we're in the outermost window
}

I suspect that, given the contents of an are different documents, they'll all report back as the root node. You might be better to check to see if document.parent is null or not.

if (window == window.parent) {
   alert("I'm not in a frame");
}

Make a function in your top document that returns it's rootNode, then call this function from your iframe documents by using the window.top reference:

in your top document:

function getRootNode()
{
 //returns the rootNode
}

In your iframe documents:

var rootNode = window.top.document.getRootNode();

try out this :

if(currentnode.parentNode == null) { alert("is root node") } 

// where currentnode is the node which you'll select

发布评论

评论列表(0)

  1. 暂无评论