Js implements the method (including browser button events) for prompting users to leave the page before they leave the page, js page

Source: Internet
Author: User

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.

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.