If I use this:
window.onunload = unloadPage;
function unloadPage()
{
alert("Hello world");
}
This would cause an alert even when going to pages within the site. Any way to use this but only when the 开发者_开发知识库user leaves the site/domain?
The short answer is that you can't. Most browsers have disabled that event.
window.onbeforeunload=confirmBrowseAway; function confirmBrowseAway() { return "If you leave this page now, unsaved input will be lost!"; }
I think Opera ignores this, but the other major/recent browsers honor it.
You can use JQuery to link a click-event to all internal links on your page. In that click event you can set a flag. In unloadPage you check for that flag and only show the alert when the flag isn't set.
精彩评论