CloseWatcher: requestClose() method
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Experimental:This is anexperimental technology
Check theBrowser compatibility table carefully before using this in production.
TherequestClose() method of theCloseWatcher interface fires acancel event and if that event is not canceled withEvent.preventDefault(), proceeds to fire aclose event, and then finally deactivates the close watcher as ifdestroy() was called.
In this article
Syntax
requestClose()Parameters
None.
Return value
None (undefined).
Examples
>Using therequestClose() method
In this example, you have your own UI component (a picker), and you want to support both the platform's default close method (e.g., theEsc key) and your custom close method (a close button).
Theonclick handler of your UI component can callrequestClose to request a close and to route your close request through the sameonclose handler the platform close method uses.
const watcher = new CloseWatcher();const picker = setUpAndShowPickerDOMElement();let chosenValue = null;watcher.onclose = () => { chosenValue = picker.querySelector("input").value; picker.remove();};picker.querySelector(".close-button").onclick = () => watcher.requestClose();Specifications
| Specification |
|---|
| HTML> # dom-closewatcher-requestclose> |