I want a JavaScript event that fires before the browser navigates to a new page

Submitted by YanSH

This idea is currently being discussed.

I want a cancellable JavaScript event that fires whenever the browser is about to open a new page, including navigations triggered by user-generated HTML content.

When hosting user-generated content written in HTML — such as comments, forum posts, or rich-text input — it is very difficult to intercept links and other elements that would cause the browser to navigate away or open a new tab. The beforeunload event fires too late and is not cancellable for cross-origin navigations, while the existing click listener approach only covers anchor tags and misses form submissions, window.open() calls, and other navigation vectors.

I want an event at the window or document level that:

  1. Fires before any page-opening navigation — links, form submissions, window.open(), and programmatic location assignments alike.
  2. Carries enough detail (destination URL, navigation type, originating element) for handlers to make an informed decision.
  3. Can be cancelled via event.preventDefault() to block the navigation entirely, or redirected to a different URL.

This would make it straightforward to sandbox untrusted HTML: a host application could intercept all outbound navigations, validate them against an allow-list, rewrite them to a safe proxy, or display an interstitial warning — all without patching every possible navigation trigger individually.

The modern Navigation API (navigate event on window.navigation) moves in the right direction for same-document navigations, but does not cover all cross-document and new-tab cases. A unified, lower-level event that covers every navigation vector would close that gap.

Tagged
JavaScript Api Dom
Votes
0
What are votes for and how are they tallied?