[英]how to identify onbeforeunload was caused by clicking close button
How do I determine if onbeforeunload was caused by clicking the close button or a page refresh or generally what event caused onbeforeunload? 如何确定onbeforeunload是由单击关闭按钮或页面刷新引起的,还是一般由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. 请帮我。
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.
在这种情况下,鼠标将不在窗口外,指向Close('X')按钮。
$(window).on('mouseover', (function () {
window.onbeforeunload = null;
}));
$(window).on('mouseout', (function () {
window.onbeforeunload = ConfirmLeave;
}));
function ConfirmLeave() {
return "";
}
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 ConfirmLeave函数将提供弹出的默认消息,如果需要自定义消息,则返回要显示的文本而不是空字符串
See if this helps, :) 看看是否有帮助,:)
As far as I know, there is no way of determining what caused the onbeforeunload
. 据我所知,无法确定导致
onbeforeunload
。 The event is triggered when window is about to close whether closing the browser or some other way. 当窗口即将关闭时,无论是关闭浏览器还是其他方式,都会触发该事件。
If the close button was pressed the value of e.clientY
is negative. 如果按下关闭按钮,则
e.clientY
值为负。 For the other possible sources i doubt there is a solution. 对于其他可能的来源,我怀疑是否有解决方案。
window.onbeforeunload = function() {
var e = window.event;
alert(e.clientX + " / " + e.clientY);
}
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.
请参阅下面的(未经测试的)代码段。
var target = $( e.target );
if(!target.is("a, :button, :submit, :input, .btn, .bulkFormButton")){
//Your code for browser events)
}
$("form").submit(function () {
//Your code for browser events)
});
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.
如果有人对他们有想法,请分享。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.