Beforeunload() does notwork on IOS - Even though the documentation says it should

Viewed 21

As I see it, beforeunload does not work using ipad/iphone. According to this page, it should work on IOS: https://developer.moWhat Mozilla Sayszilla.org/en-US/docs/Web/API/Window/beforeunload_event

But it does not work - at all - on the three IOS devices I have. Even this stackoverflow editor will not prevent loss of entered text, when used on my IPAD

I have a sample here: sublim.dk/mdnbu.html

Can anyone help me understand what is happening around onbeforeunload ?

I have tried the JQuery solutions I could find, but that did not help, either.

I have tried pagehide event. Same result ?

My sample:

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>Window: beforeunload event</title>
<body>
<p>Enter a character using Windows and pres reload. Try on IOS... Does it work ?"</p>
<textarea id="content" placeholder="Enter content for new article..." rows="4" cols="50"></textarea>
<br>
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event" target="_new">What Mozilla says</a><br>
<a href="https://www.jqueryscript.net/form/alert-form-changed-dirty.html" target="_new">What JQuery says</a>
<script>
const beforeUnloadListener = (event) => {
  event.preventDefault();
  return event.returnValue = "Are you sure you want to exit?";
};
const nameInput = document.querySelector("#content");
nameInput.addEventListener("input", (event) => {
  if (event.target.value !== "") {
    addEventListener("beforeunload", beforeUnloadListener, {capture: true});
  } else {
    removeEventListener("beforeunload", beforeUnloadListener, {capture: true});
  }
});
</script>
</body>
</html>
0 Answers
Related