Home >Web Front-end >JS Tutorial >js window closing prompt information (compatible with IE and firefox)_javascript skills

js window closing prompt information (compatible with IE and firefox)_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:35:261556browse

Many times, when browsing the web, we need to consider the situation where the user accidentally closes the window. Especially when the user has important data that has not been saved, it is necessary for our program to remind the user. At this time, we can use the javascript window.onbeforeunload() method to prevent the user from closing the window. See the following example:

<script type="text/javascript">
  window.onbeforeunload=function(event){
    return 'hello';
  }
</script>

In this way, when the user presses the close button, the following window will pop up (this is compatible with IE and Firefox):

"Hello" is the return value. When you press Cancel, the page will not be closed, only when you press OK. The return value is indispensable, even if it is empty, otherwise the page will be closed without any prompt. In fact, as long as window.close() is called, the onbeforeunload() function will be executed. From this, we can know that pressing the close button will execute the window.close() function, and window.close() also calls window.onbeforeunload() function.

With this little trick, you no longer have to worry about the damage caused by accidentally closing the window. I hope you like this article.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn