htmlurlhyperlinkuriuri-fragment

Can an URL fragment point to a HTML element that has no id?


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?


Solution

  • I think the answer simply and unfortunately is: it can not.

    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: