From 91e9b9c237ca10b234630789f1c031ccd6fe300a Mon Sep 17 00:00:00 2001 From: Ilya Kantor Date: Mon, 25 Oct 2021 22:34:12 +0300 Subject: [PATCH] minor fixes --- .../01-onload-ondomcontentloaded/article.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/2-ui/5-loading/01-onload-ondomcontentloaded/article.md b/2-ui/5-loading/01-onload-ondomcontentloaded/article.md index cd926742..2351a90c 100644 --- a/2-ui/5-loading/01-onload-ondomcontentloaded/article.md +++ b/2-ui/5-loading/01-onload-ondomcontentloaded/article.md @@ -185,6 +185,26 @@ window.onbeforeunload = function() { The behavior was changed, because some webmasters abused this event handler by showing misleading and annoying messages. So right now old browsers still may show it as a message, but aside of that -- there's no way to customize the message shown to the user. +````warn header="The `event.preventDefault()` doesn't work from a `beforeunload` handler" +That may sound weird, but most browsers ignore `event.preventDefault()`. + +Which means, following code may not work: +```js run +window.addEventListener("beforeunload", (event) => { + // doesn't work, so this event handler doesn't do anything + event.preventDefault(); +}); +``` + +Instead, in such handlers one should set `event.returnValue` to a string to get the result similar to the code above: +```js run +window.addEventListener("beforeunload", (event) => { + // same as returning from window.onbeforeunload + event.returnValue = "There are unsaved changes. Leave now?"; +}); +``` +```` + ## readyState What happens if we set the `DOMContentLoaded` handler after the document is loaded?