JavaScript browser or page off leave message

Source: Internet
Author: User

Cases

The code is as follows Copy Code

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

If you are referencing jquery, you can also use jquery for easy implementation, as follows 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 example.

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 > document.documentelement.scrollwidth-20;

if (!) ( b && Window.event.clientY < 0 | | Window.event.altKey)) {

}else{
Return "Do not close a new site, okay?" Www.111cn.net ";
}
}else{
Return "Do not close a new site, okay?" Www.111cn.net ";
}
}

Small Tips

Window.onbeforeunload only for us to leave the page or close a hint, but I test like a multi-window browser Click x Close is not prompted, and directly close the browser is not prompted, only refresh the page to be prompted OH.

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.