如何识别onbeforeunload是由点击关闭按钮引起的
如何确定 onbeforeunload 是否是由单击关闭按钮或页面刷新引起的,或者通常是什么事件导致 onbeforeunload?
这是一个片段:
window.onbeforeunload = function( e ){
if( !e ) e = window.event;
// insert code to determine which is the source of the event
}
请帮助我。
How do I determine if onbeforeunload was caused by clicking the close button or a page refresh or generally what event caused onbeforeunload?
here is a snippet:
window.onbeforeunload = function( e ){
if( !e ) e = window.event;
// insert code to determine which is the source of the event
}
Please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
参考了各种文章并进行了一些尝试和错误,最终提出了这个想法,它完全适合我,就像我希望的那样。它的逻辑也更简单,其实现的想法是检测通过关闭浏览器触发的卸载事件。在这种情况下,鼠标将离开窗口,指向“关闭”('X') 按钮。
ConfirmLeave 函数将给出弹出的默认消息,如果需要自定义消息,则返回要显示的文本而不是空字符串
看看这是否有帮助,:)
Referring to various articles and doing some trial and errors, finally developed this idea which works perfectly for me just the way i wanted it to happen. The logic was quiet simpler it implement as well The idea was to detect the unload event that is triggered by closing the browser. In that case, the mouse will be out of the window, pointing out at the Close('X') button.
The ConfirmLeave function will give the pop up default message, it case there is any need to customize the message, return the text to be displayed instead of empty string
See if this helps, :)
据我所知,没有办法确定是什么原因导致了
onbeforeunload
。当窗口即将关闭时,无论是关闭浏览器还是其他方式,都会触发该事件。As far as I know, there is no way of determining what caused the
onbeforeunload
. The event is triggered when window is about to close whether closing the browser or some other way.如果关闭按钮被按下,
e.clientY
的值为负。对于其他可能的来源,我怀疑是否有解决方案。If the close button was pressed the value of
e.clientY
is negative. For the other possible sources i doubt there is a solution.我寻找类似的东西但最终空手而归。
所以我尝试做相反的事情
我们可以识别除浏览器事件之外的所有事件。
请参阅下面的(未经测试)片段。
这对我有用,但仍有一些事件未处理。
我正在寻找那些。
如果有人对它们有想法,请分享。
I searched for something similar but ended up empty handed.
So I tried doing the opposit
We can identify all the events but browser events.
Refer below (Untested) snippet.
This worked for me but there are still some events that are not handled.
I am in search of those.
If anyone have idea about them please share.