The title may not be clearly written, indicating that the user is prompted to save the data before the user leaves the page or closes the browser, and whether to leave the page. Here we will use a window. onbeforeunload event.
Example
The Code is as follows: |
Copy code |
Window. onbeforeunload = function () {return you have unsaved content! ';} |
If you reference jquery, you can also use jquery for convenient implementation, as shown in the following js Code:
The Code is as follows: |
Copy code |
$ (Window). bind ('beforeunload', function () {return you have unsaved content! ';}); |
Let's look at a more complete instance.
The Code is as follows: |
Copy code |
Window. onbeforeunload = function (){ Var ie =! -[1,]; If (ie ){ Var n = window. event. screenX-window. screenLeft; Var B = n & gt; document.doc umentElement. scrollWidth-20;
If (! (B & amp; window. event. clientY & lt; 0 | window. event. altKey )){
} Else { Return "do not close a new website, OK? Www. bKjia. c0m "; } } Else { Return "do not close a new website, OK? Www.hzhuti.com "; } } |
Tips
Window. onbeforeunload only prompts us to leave the page or close the page, but I did not prompt to close the browser by clicking X in a multi-window browser, and there was no prompt to close the browser directly, A prompt is displayed only when the page is refreshed.