The Node.getRootNode() method returns the root of the tree that contains a node. Learn Document vs ShadowRoot results, the composed option, and what happens for detached nodes.
01
Kind
Instance method
02
Returns
Root Node
03
In page
Often Document
04
Shadow
ShadowRoot default
05
composed
Cross shadow
06
Status
Baseline widely
Fundamentals
Introduction
Every node lives in some tree. getRootNode() answers: “What is the top of my tree right now?” On a normal page that is usually the document. Inside shadow DOM it is usually the shadow root—unless you ask to look past it.
The optional { composed: true } flag is the key for shadow DOM: it returns a root beyond the shadow boundary (often still the page document).
💡
Beginner tip
Start with node.getRootNode() and check .nodeName or instanceof Document / ShadowRoot to see what you got.
Concept
Understanding getRootNode()
MDN: returns the context object’s root, optionally including the shadow root depending on options.
In a document — typically an HTMLDocument (#document).
In shadow DOM — the associated ShadowRoot when composed is false.
composed: true — a root beyond the shadow root (often the document).
Detached tree — the top node of that fragment (may be the element itself).
Node.getRootNode() is Baseline Widely available across modern browsers (MDN: since January 2020). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
Node.getRootNode()
Safe for production. Use composed: true when you need the document from inside shadow DOM.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium Edge
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerNot supported in legacy IE
No support
getRootNode()Excellent
Bottom line: Default root may be Document or ShadowRoot; composed true crosses the shadow boundary.
Wrap Up
Conclusion
Node.getRootNode() returns the root of a node’s current tree. Know the Document vs ShadowRoot cases, use composed: true to look past shadow DOM, and remember detached trees root at their topmost node.
Pass { composed: true } when you need the outer document
Check instanceof ShadowRoot / Document on the result
Handle detached fragments before append
Prefer this over manual parent loops
❌ Don’t
Assume the root is always document
Forget default composed: false inside shadow DOM
Confuse getRootNode with ownerDocument
Confuse DOM Node with the Node.js runtime
Rely on IE—this API is modern Baseline
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about getRootNode()
Find the root of the tree that currently holds a node.
5
Core concepts
🌳01
Returns root
Node at the top
API
📄02
Often Document
in the page
Light DOM
🎨03
ShadowRoot
default in shadow
Shadow
➜04
composed:true
beyond shadow
Option
🔗05
Detached
top node is root
Fragment
❓ Frequently Asked Questions
It returns the root of the tree that contains the node — typically the Document for nodes in a page, a ShadowRoot for nodes inside shadow DOM (by default), or the top node of a detached tree.
No. MDN marks Node.getRootNode() as Baseline Widely available (since January 2020). It is a standard DOM method — not Deprecated, Experimental, or Non-standard.
With composed: false (default), getRootNode stops at a shadow root. With composed: true, it crosses the shadow boundary and returns a root beyond the shadow root (often the document).
An HTMLDocument representing the page (nodeName is often #document).
It returns the root of the DOM tree they belong to. If an element has no parent, that element is its own root.
ownerDocument is the Document that created the node. getRootNode walks parents to the current tree root, which may be a ShadowRoot or a detached fragment root — not always the same as ownerDocument.
Did you know?
MDN’s Baseline date for getRootNode() is January 2020—newer than many classic Node APIs from 2015—because shadow DOM and composed roots matured later across browsers.