Node.rootNode

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

The Node.rootNode read-only property returns a Node object representing the topmost node in the tree, or the current node if it's the topmost node in the tree. This is found by walking backward along Node.parentNode until the top is reached.

For compatibility reason, this property will be renamed in the near future and no browser plans to ship it under this name.

Syntax

JavaScript
<var>rootNode</var> = node.rootNode;

Example

This example constructs a simple DOM tree of elements, then displays information about a specific node followed by the base URI of its root node.

HTML

HTML
<div id="outer">
  <div id="inner">
    <a id="button" href="go.html">
      Hello
    </a>
    <p><em id="me">It's me.</em></p>
    <p>I was wondering if after all these years, you still code in C...</p>
  </div>
</div>

<pre id="output">
</pre>

JavaScript

JavaScript
let output = document.getElementById("output");

let me = document.getElementById("me");
output.innerHTML += "Element with id 'me': " + me.outerHTML + "<br>";
output.innerHTML += "Root node's base URI: " + me.rootNode.baseURI;

Notes

Gecko-based browsers insert text nodes into a document to represent whitespace in the source markup. Therefore a node obtained, for example, using Node.firstChild or Node.previousSibling may refer to a whitespace text node rather than the actual element the author intended to get.

See Whitespace in the DOM and W3C DOM 3 FAQ: Why are some Text nodes empty? for more information.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support ? 48 (48)[1] ? ? ?
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? 48.0 (48)[1] ? ? ?

[1] Not activated by default; it is behind the pref dom.node.rootNode.enabled and  you need to set it to true to activate it.

Specifications

Specification Status Comment
DOM
The definition of 'Node.rootNode' in that specification.
Living Standard Initial definition

 

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/node/rootnode

API DOM Node Property Reference