简体   繁体   English

通过JS检测浏览器窗口/标签页关闭事件

[英]Detect via js a browser window/tab close event

I'm trying to detect if a user is leaving my websites. 我正在尝试检测用户是否离开我的网站。 Like a window/tab close event of the webbrowser or by following a link to a other domain. 就像网络浏览器的窗口/标签关闭事件一样,或者通过链接到其他域来完成。

In some searches I found some solutions, but they alert ever when a link is clicked, also on my pages. 在某些搜索中,我找到了一些解决方案,但是当我单击链接时,它们也会在我的页面上发出警报。 As example I tried out this: 例如,我尝试了这个:

window.onbeforeunload = function(event) {

event = event || window.event;

var confirmClose = 'Are you sure?';

// For IE and Firefox prior to version 4
if (event) {
   event.returnValue = confirmClose;
}

// For Safari
return confirmClose;

}

So what do I have to modify so that the alert is deactivated when I'm browsing through my domain? 那么,当我在域中浏览时,我需要做些什么修改才能使警报停用?

Thanks Oliver 谢谢奥利弗

You can't know where you are going when the onbeforeunload fires. onbeforeunload触发时,您不知道要去哪里。 And the purpose is basically to prevent exactly what you are trying to achieve. 目的基本上是为了防止您要达到的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM