Current specifications and implementations allow a URL to specify a target in the URL fragment. If the navigated to resource has:
- an element with an id matching the fragment
- a link with with a name attribute that matches the fragment
that element will be the CSS target. The UA will scroll the targeted element into view when the resource is loaded. This allows linking to specific parts of a resource. For example, the Wikipedia entry on “Cat” is very long and broad. However, each subsection is marked with an id so users and resources can link directly to a subsection, for example: Cat Behavior.
However, this relies on a page author predicting all the parts of a resource users may find interesting and marking it up with ids. This limitation makes it difficult to use fragments to link to arbitrary content. For example, take the page: https://www.gutenberg.org/files/2147/2147-h/2147-h.htm. Suppose a user or resource wants to link to the paragraph (e.g. to cite a quote) that starts with:
“It became necessary, at last, that I should arouse both master and valet to the expediency of removing the treasure.”
The referring page would have to link to “THE GOLD-BUG” section and tell the user to scroll down or use their browser’s find-in-page function.
I’d like to propose extending the URL fragment for HTML documents to allow specifying a text snippet as the target. In this case, we could encode the above quote directly in the URL fragment and the user agent would scroll directly to the specified text, possibly highlighting it to the user.
I have an explainer in my personal GitHub repo that has more details and a proposed solution.
Would anyone else be interested in discussing and iterating on this proposal in the WICG?