Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fragmentDirective - live examples #35716

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions files/en-us/web/api/document/fragmentdirective/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,38 @@ A {{domxref("FragmentDirective")}} object.

## Examples

Try running the following in a supporting browser's devtools, in a tab with one or more matched text fragments:
### Checking if text fragments are supported

The code below logs whether or not text fragments are supported in your browser by checking for existence of the object.
Note that the object is empty, and at present it is mainly intended for feature detection.
In the future, it might include other information.

```html hidden
<pre id="log"></pre>
```

```js hidden
const logElement = document.querySelector("#log");
function log(text) {
logElement.innerText = text;
}
```

```css hidden
#log {
height: 20px;
}
```

```js
document.fragmentDirective;
// returns an empty FragmentDirective object, if supported
// undefined otherwise
if (document.fragmentDirective) {
log("Your browser supports text fragments.");
} else {
log("Text fragments are not supported in your browser.");
}
```

This functionality is mainly intended for feature detection at present. In the future, the `FragmentDirective` object could include additional information.
{{EmbedLiveSample("Checking if text fragments are supported","100%","30px")}}

## Specifications

Expand Down
35 changes: 29 additions & 6 deletions files/en-us/web/api/fragmentdirective/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ browser-compat: api.FragmentDirective

{{APIRef("URL Fragment Text Directives")}}{{SeeCompatTable}}

The **`FragmentDirective`** interface is an object exposed for feature detectability, that is, whether or not a browser supports text fragments.
The **`FragmentDirective`** interface is an object exposed to allow code to check whether or not a browser supports text fragments.

It is accessed via the {{domxref("Document.fragmentDirective")}} property.

Expand All @@ -23,15 +23,38 @@ None.

## Examples

Try running the following in a supporting browser's devtools, in a tab with one or more matched text fragments:
### Checking if text fragments are supported
hamishwillee marked this conversation as resolved.
Show resolved Hide resolved

The code below logs whether or not text fragments are supported in your browser by checking that {{domxref("Document.fragmentDirective")}} is defined.
Note that the object is empty, and at present is mainly intended for feature detection.
In the future, it might include other information.

```html hidden
<pre id="log"></pre>
```

```js hidden
const logElement = document.querySelector("#log");
function log(text) {
logElement.innerText = text;
}
```

```css hidden
#log {
height: 20px;
}
```

```js
document.fragmentDirective;
// returns an empty FragmentDirective object, if supported
// undefined otherwise
if (document.fragmentDirective) {
log("Your browser supports text fragments.");
} else {
log("Text fragments are not supported in your browser.");
}
```

This functionality is mainly intended for feature detection at present. In the future, the `FragmentDirective` object could include additional information.
{{EmbedLiveSample("Checking if text fragments are supported","100%","30px")}}

## Specifications

Expand Down