Javascript blocking window close, compatible with IE and firefox

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.