IT lexicon › Web › CloseWatcher

CloseWatcher

Web På svenska → Updated: 2026-07-29

A single API for "the user wants to close this", whether the signal is Escape on a keyboard or the back gesture on a phone.

The problem arose because the two gestures were handled entirely differently. A modal on the desktop listens for Escape, but on Android the natural closing gesture is the back button — which in a browser means navigating back through history. For years the workaround was to push a fake history entry when the dialog opened and listen for it being popped, a hack that made bookmarks, deep links and the actual back button unreliable. With new CloseWatcher() you instead get a close event for both gestures, plus a cancel event to hook into if there is unsaved work to ask about. The <dialog> element and popovers use the mechanism internally, so most people get the behaviour for free.

← Back to the lexicon