Js implements the method (including browser button events) for prompting users to leave the page before they leave the page, js page
The example in this article describes how js prompts users to exit the page before leaving the page (including browser button events ). Share it with you for your reference. The details are as follows:
Before you exit the page, you are prompted to leave the page (including browser button events)
<Script type = "text/javascript"> window. onbeforeunload = function () {return "your article has not been saved! ";}</Script>
If a dialog box is displayed when you exit the page, the user is prompted to exit the page. This is similar to setting a function that does not save the page. This implementation method is relatively simple. The most common method is to use the unload event. However, this implementation has the disadvantage that the page will exit regardless of whether the result is the same or not. Therefore, if you want to bring up a dialog box, you can select a space. If you are sure, exit. Otherwise, the page will not be closed.
We recommend that you use the onbeforeunload () event, which means the method to be executed before loading the unload event. Use the following:
<Script type = "text/javascript"> window. onbeforeunload = function () {return "are you sure you want to exit the page? ";}</Script>
In this case, a dialog box is displayed, and the page is exited only after confirmation.
Window. onbeforeunload can be combined with jQuery to implement the prompt effect when the page is set but not saved. That is, if the page is saved, the dialog box should not be displayed. You can use the following method.
Prompt dialog box:
<script type="text/javascript"> window.onbeforeunload = function(){ return; }</script>
In this way, the page is exited without any dialog box.
In combination with jQuery, the Javascript exit page pop-up dialog box is used in combination with the above two methods. When the page meets certain conditions, if a dialog box needs to pop up, otherwise the dialog box is not displayed, which is very convenient, jQuery has its own unload method, but it cannot return the same result. There is only one result, that is, exit the page. Therefore, combined with window. onbeforeunload can be used to exit the dialog box.
I hope this article will help you design javascript programs.