Javascript browser or page close exit prompt information

Source: Internet
Author: User

The title may not be clearly written, indicating that the user is prompted to save the data before the user leaves the page or closes the browser, and whether to leave the page. Here we will use a window. onbeforeunload event.

Example

The Code is as follows: Copy code

Window. onbeforeunload = function () {return you have unsaved content! ';}

If you reference jquery, you can also use jquery for convenient implementation, as shown in the following js Code:

The Code is as follows: Copy code

$ (Window). bind ('beforeunload', function () {return you have unsaved content! ';});

Let's look at a more complete instance.

The Code is as follows: Copy code

Window. onbeforeunload = function (){
Var ie =! -[1,];
If (ie ){
Var n = window. event. screenX-window. screenLeft;
Var B = n & gt; document.doc umentElement. scrollWidth-20;

If (! (B & amp; window. event. clientY & lt; 0 | window. event. altKey )){

} Else {
Return "do not close a new website, OK? Www. bKjia. c0m ";
}
} Else {
Return "do not close a new website, OK? Www.hzhuti.com ";
}
}

Tips

Window. onbeforeunload only prompts us to leave the page or close the page, but I did not prompt to close the browser by clicking X in a multi-window browser, and there was no prompt to close the browser directly, A prompt is displayed only when the page is refreshed.

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.