简体   繁体   English

如何识别onbeforeunload是由单击关闭按钮引起的

[英]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.

相关问题 如何确定点击Chrome中的链接是否导致onbeforeunload - How to determine if onbeforeunload has been caused by clicking a link in Chrome 如何通过单击按钮自行关闭网页 - How to close the webpage on its own on clicking a button 如何区分两个“onpause”事件——由点击“pause”按钮引起,以及由到达媒体片段末尾引起? - How to distinguish between two “onpause” events - caused by clicking “pause” button, and caused by reaching the end of a media fragment? 如何确定JavaScript中window.onbeforeunload中的哪个控件导致了该事件 - How to determine which control in window.onbeforeunload in javascript caused the event 仅单击按钮即可折叠/关闭按钮 - Collapse/close a button only by clicking on it 如何通过在打开按钮区域之外单击来关闭菜单? - How to close a menu by clicking outside the area of the open button? 如何在不单击“取消”按钮的情况下关闭Ionic2 Datetime弹出窗口 - How to close Ionic2 Datetime popup without clicking Cancel button jQuery - 如果在div外部单击或打开/关闭按钮,如何删除类 - jQuery - How to remove class if clicking outside of div or open/close button 点击提交按钮后如何关闭灯箱表单? - How to close light box form after clicking on submit button? 单击“X”按钮时如何关闭弹出窗口并停止视频 - How to close popup and stop video when clicking "X" button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM