Text fragments in Firefox

Firefox, Firefox for Android Firefox, Firefox for Android 最后更新于: 1 day, 14 hours ago
我们的志愿者们正在努力翻译这篇文章。在翻译完成前,英文版本也许可以帮上忙。如果您想来帮忙翻译,请点此了解

Text fragments enable users to link directly to a specific portion of text within a web document without requiring the document’s author to annotate the text with an ID. This feature leverages a unique syntax in the URL fragment to identify and highlight the target text.

This enables you to create links that highlight a specific portion of text on a webpage. When someone clicks your link, the browser navigates to the webpage and automatically scrolls to and highlights the desired text.

How to Use Text Fragments

One of the web’s most fundamental features is the ability to link between different documents. There are two primary ways to create links:

Linking to the Top of a Document by providing the document's URL

link to the top

Linking to a Specific Section of a Document by appending a document fragment (ID) to the URL

link specific

Create and use Text Fragments in Firefox

  1. Identify the Text to Highlight. Choose the exact text you want users to see on the target webpage.
  2. Create the URL. Use the following syntax to add a Text Fragment to the URL:
    link step 2
text= A text directive. This provides a text fragment to the browser, defining what text is to be linked to in the linked document.
textStart A text string specifying the start of the linked text. When used without textEnd it will contain the whole range that must be highlighted.
textEnd A text string specifying the end of the linked text. When textStart and textEnd are both present, they serve as boundary points of a range.
prefix- A text string followed by a hyphen specifying what text should immediately precede the linked text, only allowing for whitespace in between. This helps the browser to select the correct linked text, in cases where there are multiple matches.
-suffix A hyphen followed by a text string specifying what text should immediately follow the linked text, only allowing for whitespace in between. This helps the browser to select the correct linked text, in cases where there are multiple matches.
Note: The text strings used for the following parameters must be percent-encoded: textStart, textEnd, prefix, suffix-.
For example, this highlights the phrase "Firefox Text Fragments" on the page:
Text fragment example

Test Your Link: Open the link in Firefox to ensure it highlights the intended text. If the webpage content changes or multiple matches are present, adjust the fragment using a prefix or suffix for greater accuracy.

Practical Examples

  1. Highlight a Single Word:
    practical 1
    This highlights the word "example."
  2. Highlight a Range of Text:
    practical 2
    This highlights the text "Text Fragments are useful."
  3. Add Context for Ambiguous Matches:
    practical 3
    This ensures the specific phrase is highlighted even if similar text exists on the page.
Note: While Firefox supports Text Fragments, ensure your recipients use a compatible browser to benefit from this feature. Supported Browsers are Firefox, Chrome, Edge, Safari and Opera.

Common Questions

What happens if the target text changes?

The Text Fragment might fail if the target text is removed or altered significantly. Use prefixes and suffixes to improve resilience. If matching fails, the page behaves as if there is no Text Fragment. If an additional document fragment is present, it is scrolled to, otherwise the page scrolls to the top.

Can I use Text Fragments with non-text elements?

No. Text Fragments only work with selectable text, not images, videos, or dynamic elements.

Do all users see the highlight?

Yes, as long as their browser supports Text Fragments.

What are the limitations of linking to document fragments?

Linking to document fragments requires the page author to define an anchor, such as an id attribute, in the HTML.

If the ID is modified or removed, the link breaks and the user is redirected to the top of the page. While this behavior is an example of graceful degradation, it limits the control of the link author. The ability to link precisely to specific content is at the mercy of the page author’s decisions.

这篇文章对您有帮助吗?

请稍候...

此文章在这些用户的协助下写成:

Illustration of hands

志愿者

分享知识并培养专业技能。解答问题并改进我们的知识库。

详细了解