Document.fragmentDirective is a read-only instance property that returns a FragmentDirective object. Today it is mainly for feature-detecting text fragments (scroll-to-text links). Learn the URL syntax, why the object is empty, ::target-text styling, and five examples with try-it labs.
01
Kind
Read-only property
02
Returns
FragmentDirective
03
Purpose
Feature detect
04
Methods
None today
05
Related CSS
::target-text
06
Status
Baseline 2025
Fundamentals
Introduction
Classic links jump to an element id with #section. Text fragments go further: a special URL fragment can tell the browser to find a phrase on the page, scroll to it, and highlight it—even when the author never added an id.
MDN: document.fragmentDirective returns the FragmentDirective for the current document. The object is currently empty and is mainly intended for feature detection; in the future it might include other information.
💡
Baseline 2025 — newly available
MDN marks this property Baseline 2025 (newly available since March 2025). It is not listed as Deprecated, Experimental, or Non-standard, but older browsers may still lack it—always feature-detect.
A read-only instance property on Document. Its value is a FragmentDirective object (MDN).
Value — FragmentDirective for this document.
Empty today — no instance properties or methods listed on MDN.
Main use — detect text fragment support via existence of the object.
Future-ready — MDN notes it might hold more information later.
Not for reading matches — text directives are stripped so scripts cannot directly interact with them (MDN Text fragments).
Foundation
📝 Syntax
JavaScript
document.fragmentDirective
Value
A FragmentDirective object (MDN).
Feature detection (MDN)
JavaScript
if (document.fragmentDirective) {
console.log("Your browser supports text fragments.");
} else {
console.log("Text fragments are not supported in your browser.");
}
URL feature
🔗 What text fragments look like
A text fragment uses a fragment directive after #:~:. The simplest form highlights a single phrase:
More advanced forms can use start/end context and multiple directives. Supporting browsers scroll to the match and apply a highlight you can style with ::target-text.
Compare
⚖️ Related pieces of the feature
Piece
Role
document.fragmentDirective
JS feature detection (MDN)
FragmentDirective
Empty interface today; may grow later
#:~:text=...
URL syntax that triggers scroll + highlight
::target-text
CSS to style the highlighted match
#element-id
Classic fragment to an element id
Styling
🎨 Style highlights with ::target-text
When a text fragment match is shown, authors can customize the highlight with the CSS ::target-text pseudo-element (MDN).
Log whether the browser exposes document.fragmentDirective.
JavaScript
if (document.fragmentDirective) {
console.log("Your browser supports text fragments.");
} else {
console.log("Text fragments are not supported in your browser.");
}
Document.fragmentDirective is marked Baseline 2025 newly available on MDN (since March 2025). Older browsers may lack it — feature-detect. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline 2025 · Newly available
Document.fragmentDirective
FragmentDirective marker for text fragment support — empty object used for feature detection.
2025Newly available
Google ChromeModern versions · verify
Supported (modern)
Mozilla FirefoxModern versions · verify
Supported (modern)
Apple SafariModern versions · verify
Supported (modern)
Microsoft EdgeChromium modern · verify
Supported (modern)
OperaFollow Chromium modern path
Supported (modern)
Internet ExplorerNot supported
No support
Document.fragmentDirectiveBaseline 2025
Bottom line: Use if (document.fragmentDirective) before offering text-fragment deep links. Style highlights with ::target-text, and keep classic #id links as a fallback.
Wrap Up
Conclusion
Document.fragmentDirective is a small but useful Baseline 2025 API: an empty FragmentDirective whose presence means the browser understands text fragments. Detect first, share #:~:text= links carefully, and style highlights with ::target-text.
Expect properties/methods on FragmentDirective today
Assume scripts can read the text directive from location
Skip detection on older devices (Baseline 2025 is new)
Rely only on text fragments for critical navigation
Confuse :target (element) with ::target-text
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about document.fragmentDirective
Empty FragmentDirective — detect text fragment support.
5
Core concepts
📄01
Returns
FragmentDirective
API
✓02
Status
Baseline 2025
MDN
🔎03
Use
feature detect
Primary
🔗04
URL
#:~:text=
Deep link
🎨05
Style
::target-text
CSS
❓ Frequently Asked Questions
A FragmentDirective object for the current document (MDN). Today the object is empty and is mainly used to feature-detect text fragment support.
MDN does not mark Document.fragmentDirective as Deprecated, Experimental, or Non-standard. It is Baseline 2025 newly available (since March 2025) — support may be missing in older browsers.
URL fragment directives like #:~:text=Hello that tell the browser to scroll to and highlight matching page text without needing an element id.
Text fragment directives are stripped during loading so author scripts cannot directly interact with them (MDN Text fragments). Use fragmentDirective only for support detection.
Use the CSS ::target-text pseudo-element to customize the highlight appearance when the browser scrolls to a text fragment.
No. MDN lists no instance properties or methods on FragmentDirective today — existence of the object is the signal.
Did you know?
The :~: marker separates normal URL fragments from user-agent instructions (directives). That design lets browsers strip text fragments before page scripts run—so deep links stay useful without leaking the matched phrase into author JavaScript (MDN).