Before the user leaves the page, prompts to leave this page (including browser button events)

Source: Internet
Author: User

Before the user leaves the page, prompts to leave this page (including browser button events)


<script type= "Text/javascript" > window.onbeforeunload = function () {
Return "Your article has not been saved. ";
}

</script>


If you need to eject a dialog box when you exit the page, prompt the user to exit the page, similar to setting a feature without saving the page. This implementation of the method is relatively simple, the most common is the use of unload events, but this implementation has a disadvantage, is whether to agree or not, the result is the same, will exit the page, therefore, if you want to pop-up dialog box, there are users can choose the space, if you are sure to exit, otherwise do not close the page, the effect chart is as follows:



The onbeforeunload () event is recommended here, which means that the method executed before loading the Unload event is used as follows:

<script type= "Text/javascript" >

Window.onbeforeunload = function () {
Return "Will you be sure you want to exit the page?" ";

}
</script> This will pop up a dialog box, only confirmation will exit the page.

Window.onbeforeunload can be combined with jquery implementation when the user set the page without saving the hint effect, that is, if the saved should not prompt the dialog box, you can use the following methods, when you do not need

Prompt dialog box:

<script type= "Text/javascript" >

Window.onbeforeunload = function () {

Return
}

</script> This will exit the page directly without any dialog boxes popping up.

Combining jquery to implement JavaScript exit page Pop-up dialog box is the combination of the above two methods, when the page satisfies a situation, if you need to pop up a dialog box pops up, otherwise not pop-up dialog box, so very convenient, jquery has its own Unload method, but also can not achieve return , there is only one result, that is to exit the page, so combining window.onbeforeunload will be able to implement an Exit page Pop-up dialog box function.

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.