Prompt user for unsaved changes when leaving webpage

前端 未结 2 1955
鱼传尺愫
鱼传尺愫 2020-12-20 05:54

What options do I have preventing the user from just closing the browser or navigating to another site? (of course I can\'t prevent him pulling the plug on the computer, etc

相关标签:
2条回答
  • 2020-12-20 06:23

    You should use the onunload event.

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

    You can see an example here: http://www.w3schools.com/jsref/jsref_onunload.asp

    0 讨论(0)
  • 2020-12-20 06:31

    You could use the JS beforeunload event in order to achieve this.

    0 讨论(0)
提交回复
热议问题