Most of the time, when browsing a Web page, we need to consider the situation where users close the window in a few hours, especially when the user has important data that has not been saved, it is necessary for our program to remind users. In this case, we can use javascript window. onbeforeunload () to prevent users from closing the window. See the following example:
<Script type = "text/javascript"> window. onbeforeunload = function (event) {return 'hello' ;}</script>
In this way, when you press the close button, the following window will pop up (compatible with IE and firefox ):
"Hello" is the return value. When you press Cancel, the page will not be closed. Only after you press OK. The return value is indispensable, even if it is null, otherwise the page will be closed without any prompts. In fact, as long as the window is called. close () will execute the onbeforeunload () function, so we can know that the window will be executed by pressing the close button. close () function, while window. in close (), window is also called. onbeforeunload () function.
Articles you may be interested in
- Js shields mouse and keyboard events (including right-click, direction key, backspace key, F5 refresh key, etc.), compatible with IE and firefox
- Javascript shield right-click, compatible with IE and firefox
- Javascript determines whether the mouse is left or right-click-compatible with ie, firefox, chrome, and other major browsers
- Js can only enter numbers and numbers with decimal points, compatible with IE and firefox
- Js obtains the height of the scroll bar from the top and bottom of the browser. It is compatible with ie and firefox.
- Js shield direction keys, compatible with IE and firefox
- Js obtains the number of rows and columns of a table (compatible with IE and firefox)
- Javascript to get the current mouse position (compatible with IE and firefox)