Skip to content

Browser does not change focus to hash fragment given by Hx-Location response header #3448

@nmattela

Description

@nmattela

Hi,

Before creating a PR with a possible fix I have in mind, I first want to know whether this is a bug or a feature.
I am looking to implement a "locate" feature, wherein one can locate (and highlight) an element in a long, paginated list by its id. To achieve this, the user first searches the element by its ID, which sends a request to the server. The server will look up what page the element belongs to, then returns a URL that contains the correct page number as a query parameter. This URL is provided via the Hx-Location header. The server will also add a hash fragment to the URL with the element's ID, so that the browser can then automatically scroll to and highlight that element on the page.
This last part is where there's a problem: HTMX will use history.pushState to push this new URL onto the history stack of the browser, including the hash fragment. However, history.pushState does NOT signal the browser that the hash fragment has changed, and that the browser should focus on the associated element. Other methods such as window.location = "<location given by Hx-Location>" will trigger the change.
Is this behaviour a conscious design decision or not? Cause if not, when hx-push-url or hx-replace-url are set to true, the pushUrlIntoHistory function can be extended by also updating window.location to trigger the hash change.

As a sidenote: this behaviour used to be possible with using the Hx-Redirect response header. However, as of a recent Firefox update, this is no longer possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions