Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if document is ROOT node

Tags:

javascript

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.

like image 411
Van Coding Avatar asked Feb 04 '11 08:02

Van Coding


People also ask

How do I get the root node in HTML?

getRootNode() method. The getRootNode() method returns the node object's root node, it optionally also includes the shadow root if it is available. Composed: A Boolean that indicates if the shadow root should be returned (false), or a root node beyond shadow root (true).

What is the root node in Dom?

In your example, rootNode is the HTML element from which to start recursively searching the hierarchical DOM tree (which is how the web page is represented in JavaScript) for elements with class name cn . This allows the caller of your function to specify from where they want to search for elements with class name cn .

What is document documentElement?

documentElement returns the Element that is the root element of the document (for example, the <html> element for HTML documents).


1 Answers

You should be able to do this with top:

if (window.top.document === window.document) {
    // we're in the outermost window
}
like image 145
lonesomeday Avatar answered Oct 03 '22 10:10

lonesomeday