JavaScript Document fragmentDirective Property

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline 2025
Instance property

What You’ll Learn

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

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.

Related Document tutorials: forms, fonts, Document constructor.

Understanding Document.fragmentDirective

A read-only instance property on Document. Its value is a FragmentDirective object (MDN).

  • ValueFragmentDirective 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).

📝 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.");
}

🔗 What text fragments look like

A text fragment uses a fragment directive after #:~:. The simplest form highlights a single phrase:

JavaScript
https://example.com/page#:~:text=important%20phrase

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.

🎨 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).

JavaScript
::target-text {
  background-color: rebeccapurple;
  color: white;
}

⚡ Quick Reference

GoalCode / note
Detect supportif (document.fragmentDirective) { ... }
Get the objectdocument.fragmentDirective
Expect methods?None today (MDN)
Share a deep link#:~:text=Your%20phrase
Style highlight::target-text { ... }
MDN statusBaseline 2025 (newly available)

🔍 At a Glance

Four facts about document.fragmentDirective.

Type
FragmentDirective

Read-only

Shape
empty

Detect only

Enables
#:~:text=

Deep links

Status
Baseline 2025

Newly available

📋 Text fragment vs element id fragment

#heading#:~:text=...
Needs author markup?Yes (id)No
Scrolls toElementMatching text
HighlightOptional via :targetBuilt-in + ::target-text
Visible to page JS?Usually in location.hashDirective stripped (MDN)
Detect supportAlways classicdocument.fragmentDirective

Examples Gallery

Examples follow MDN Document: fragmentDirective and the FragmentDirective interface. Use View Output or Try It Yourself for each case.

📚 Getting Started

Feature-detect text fragment support the MDN way.

Example 1 — MDN: Check Text Fragment Support

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.");
}
Try It Yourself

How It Works

Truthiness of the object is enough—you do not call methods on it today.

Example 2 — Inspect Type Safely

Combine in / typeof style checks for clearer diagnostics.

JavaScript
const supported = "fragmentDirective" in document;
console.log("in document?", supported);
console.log("typeof:", typeof document.fragmentDirective);
console.log("value:", document.fragmentDirective);
Try It Yourself

How It Works

On unsupported engines, "fragmentDirective" in document is false.

📈 Empty Object, Share URLs & CSS

See that the API is a marker today, then style and share links.

Example 3 — The Object Is Empty Today (MDN)

Confirm there are no enumerable own keys to read.

JavaScript
if (!document.fragmentDirective) {
  console.log("Not supported");
} else {
  const keys = Object.keys(document.fragmentDirective);
  console.log("own keys:", keys);
  console.log("keys length:", keys.length); // 0 today
}
Try It Yourself

How It Works

MDN: mainly for feature detection; future versions might expose more data.

Example 4 — Build a Text Fragment Share Link

Create a deep-link string your users can open in a supporting browser.

JavaScript
function textFragmentUrl(pageUrl, phrase) {
  const encoded = encodeURIComponent(phrase);
  return `${pageUrl}#:~:text=${encoded}`;
}

const link = textFragmentUrl(
  "https://example.com/article",
  "important phrase"
);
console.log(link);
// https://example.com/article#:~:text=important%20phrase
Try It Yourself

How It Works

Gate “Copy deep link” UI with document.fragmentDirective so you only offer it where it works.

Example 5 — Style Matches with ::target-text

Customize the highlight when a text fragment is active.

JavaScript
::target-text {
  background-color: rebeccapurple;
  color: white;
}
JavaScript
// CSS does the visual work when a #:~:text= link is opened.
// JS only detects support:
console.log(!!document.fragmentDirective);
Try It Yourself

How It Works

Open a page with a text fragment link to see the pseudo-element apply in a supporting browser.

🚀 Common Use Cases

  • Feature detection — show “Copy text link” only when supported.
  • Docs / knowledge bases — share deep links to exact sentences.
  • Search result previews — open articles at the matched quote.
  • Design polish — brand the highlight with ::target-text.
  • Progressive enhancement — fall back to classic #id links.
  • Not for reading the match in JS — directives are stripped from the URL (MDN).

🧠 How Text Fragments Relate to This Property

1

User opens a #:~:text= URL

The fragment carries a user-agent text directive.

Link
2

Browser strips the directive

Author scripts cannot directly read it from the URL (MDN).

Privacy
3

Browser finds and highlights text

Scroll + highlight; CSS ::target-text can restyle it.

Render
4

JS only checks fragmentDirective

Existence of the object answers “does this browser support text fragments?”

📝 Notes

  • MDN: Baseline 2025 newly available (since March 2025) — no Deprecated / Experimental / Non-standard banner.
  • Object is empty today; mainly for feature detection (MDN).
  • FragmentDirective lists no properties or methods (MDN).
  • Style matches with ::target-text.
  • Related: forms, fonts, Document constructor.

Browser Support

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.

2025 Newly available
Google Chrome Modern versions · verify
Supported (modern)
Mozilla Firefox Modern versions · verify
Supported (modern)
Apple Safari Modern versions · verify
Supported (modern)
Microsoft Edge Chromium modern · verify
Supported (modern)
Opera Follow Chromium modern path
Supported (modern)
Internet Explorer Not supported
No support
Document.fragmentDirective Baseline 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.

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.

Continue with fullscreen, forms, fonts, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Feature-detect with document.fragmentDirective
  • Offer classic #id fallbacks for older browsers
  • Style highlights via ::target-text
  • URL-encode phrases in #:~:text= links
  • Treat the API as a capability flag for now

❌ Don’t

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about document.fragmentDirective

Empty FragmentDirective — detect text fragment support.

5
Core concepts
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).

Next: fullscreen

Learn why document.fullscreen is deprecated and what to use instead.

fullscreen →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful