I want the "Changes you have made may not be saved?" popup to show up when a user tries to close a tab without properly exiting, how do I make sure it shows up?
Asked
Active
Viewed 437 times
-2
-
You should not prevent a user from ever closing a tab. – Jeff B Mar 29 '21 at 15:16
1 Answers
3
Check here : Confirmation before closing of tab/browser
You may what to check some stuff on the window.onbeforeunload
event.
Edit:
I copied and pasted the solution from yaya into my html which will at lease use the browsers default question if you are sure to leave. Full snippet:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<script>
var ask = true
window.onbeforeunload = function (e) {
if (!ask) return null
e = e || window.event;
//old browsers
if (e) { e.returnValue = 'Sure?'; }
//safari, chrome(chrome ignores text)
return 'Sure?';
};
</script>
</body>
</html>

Matthias
- 485
- 7
- 21