开发者

How to give javascript alert to user on leaving the site/Domain?

开发者 https://www.devze.com 2023-01-25 11:12 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消