User request for unsaved changes when exiting a web page.

What options do I have to prevent the user from just closing the browser or switching to another site? (of course, I can’t stop him from pulling the plug onto the computer, etc .: I just want to show him a warning)

+1
source share
2 answers

To achieve this, you can use the JS event beforeunload.

+6
source

You must use the onunload event.

<script>
function onExitHandler() {
  // called when user about to leave the page
}
</script>
<body onunload="onExitHandler()">
...
</body>

Here you can see an example: http://www.w3schools.com/jsref/jsref_onunload.asp

+1
source

Source: https://habr.com/ru/post/1615229/


All Articles