2

I'd like to provide a link to an external page (not under my control) that has a specific part where I want it to point to (it's a license page with an appendix, and I want the link to go directly to the appendix).

I would normally do this with a # in the URL, like this: https://en.wikipedia.org/wiki/Fragment_identifier#Examples

This makes the page scroll to already start on the HTML element with id="Examples".
The problem is when the HTML page doesn't put an id on the elements.

Is there any other way to make the URL point to the relevant section? Do URL fragments accept only ids or can I use other CSS selectors somehow?

4
  • Can you give me the html of your section ?
    – saifur
    May 29, 2019 at 18:21
  • Oh, I looked on the page today, and found an id now (I think it wasn't there before when I looked months ago). So my problem is solved, but the question remains: is it possible to reference an HTML element in the URL without being by id?
    – geekley
    May 29, 2019 at 19:16
  • So I assume that without an id it's not possible then. I wonder if the behavior of "browsers scrolling to show the element identified in a URL fragment" is just a convention that browsers happen to follow or if it's expected behavior (defined in a spec or something).
    – geekley
    Jun 1, 2019 at 20:21

1 Answer 1

2

I think the answer simply is: you cannot.

An id attribute on elements is required (barring very specific JavaScript features on your target page, and barring extensions that inject stuff after the fact) to link to a specific element on the page.

The scrolling behavior is defined in the spec, under section "Scrolling to a fragment":

To find a potential indicated element given a Document document and a string fragment, run these steps:

  1. If there is an element in the document tree whose root is document and that has an ID equal to fragment, then return the first such element in tree order.
  2. If there is an a element in the document tree whose root is document that has a name attribute whose value is equal to fragment, then return the first such element in tree order.
  3. Return null.

Notes:

  • The name attribute for a is not valid in current HTML5 anymore.
  • The empty # fragment (as well as the #top fragment, if no top id is defined on the page) can be used to scroll to the top of the page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.