document.moveBefore(movedNode, referenceNode) moves a node so it becomes a direct child of the Document, placed before referenceNode (or at the end when referenceNode is null). Unlike insertBefore(), it does not remove-and-reinsert — so focus, animations, and similar state can survive the move (MDN).
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call document.moveBefore. Start with this line.
JavaScript
document.moveBefore(movedNode, referenceNode)
Call
What it does
document.moveBefore(movedNode, referenceNode)
Primary signature.
document.moveBefore(...)
Call with the arguments your case needs.
Built into the browser. Call moveBefore(...) on document — no import required.
Library declarations
JavaScript
document.moveBefore(movedNode, referenceNode)
Parameters
Parameter
Description
movedNode
Required. Element or CharacterData to move
referenceNode
Required. Insert before this Document child; null = end
Return Value
Return Value
Description
*
undefined
JavaScript Examples
Example 1 — Feature-Detect First
Limited availability means you must check before calling.
JavaScript
if (typeof document.moveBefore === "function") {
console.log("document.moveBefore is available");
} else {
console.log("missing — use insertBefore() or Element.moveBefore()");
}
moveBefore
(or insertBefore on unsupported browsers)
How It Works
Prefer the new API. When present, you keep state-preserving moves.
Fall back safely.insertBefore is Baseline and widely understood.
Pass a real node. Create a comment, then move it under Document with null.
Constraints. Same-document / connectedness rules still apply to moveBefore (MDN).
Compatibility
Browser Support
Document.moveBefore() is Limited availability on MDN (not Baseline). Support includes recent Chromium and Firefox; Safari currently lacks support. See the MDN moveBefore() reference.
✓ Limited availability
Document.moveBefore()
State-preserving move under the Document node. Feature-detect and fall back to insertBefore().
LimitedNot Baseline
Google Chrome133+
Yes
Microsoft Edge133+
Yes
Mozilla Firefox144+
Yes
Opera118+
Yes
Apple SafariNot supported
No
Internet ExplorerNot supported
No
moveBefore()Partial
Bottom line: Use document.moveBefore for rare Document-level moves. For UI, prefer Element.moveBefore with an insertBefore fallback.
Frequently Asked Questions
It moves a Node to become a direct child of the Document, before a given reference node. Unlike insertBefore(), it does not remove and reinsert the node, so state such as focus and animations can be preserved (MDN).
No. MDN does not mark Document.moveBefore() as Deprecated, Experimental, or Non-standard. It is Limited availability (not Baseline) because some major browsers do not support it yet.
None (undefined) (MDN).
If referenceNode is null, movedNode is inserted at the end of the Document's child nodes (MDN).
MDN notes it is not particularly useful on Document itself. For moving elements between containers while keeping focus or animations, prefer Element.moveBefore() (or DocumentFragment.moveBefore()).
When movedNode is not part of this document, is not Element/CharacterData, you try to move before the doctype, or connectedness constraints fail. Use insertBefore() or try/catch when those cases matter (MDN).