demo · v135

Which element navigated me?

Chrome 135 adds sourceElement to NavigateEvent so the navigation.addEventListener("navigate", …) handler knows whether the navigation came from a specific <a>, <form>, or programmatic call. Click each trigger below and see what the handler reports.

Navigation API: ?
link a (href=#anchor-a) link b (href=#anchor-b)

last NavigateEvent

waiting…

the code

navigation.addEventListener("navigate", (e) => {
  console.log({
    url: e.destination.url,
    sourceElement: e.sourceElement?.tagName,
    sourceElementId: e.sourceElement?.id,
    userInitiated: e.userInitiated,
  });
});

see also