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

发布于 2024-09-24 06:20:56 字数 267 浏览 5 评论 0原文

如何确定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小红帽 2024-10-01 06:20:56

参考了各种文章并进行了一些尝试和错误,最终提出了这个想法,它完全适合我,就像我希望的那样。它的逻辑也更简单,其实现的想法是检测通过关闭浏览器触发的卸载事件。在这种情况下,鼠标将离开窗口,指向“关闭”('X') 按钮。

$(window).on('mouseover', (function () {
    window.onbeforeunload = null;
}));


$(window).on('mouseout', (function () {
    window.onbeforeunload = ConfirmLeave;
}));

function ConfirmLeave() {
    return "";
}

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.

$(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

See if this helps, :)

顾北清歌寒 2024-10-01 06:20:56

据我所知,没有办法确定是什么原因导致了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.

骄傲 2024-10-01 06:20:56

如果关闭按钮被按下,e.clientY 的值为负。对于其他可能的来源,我怀疑是否有解决方案。

window.onbeforeunload = function() {
   var e = window.event;
   alert(e.clientX + " / " + 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.

window.onbeforeunload = function() {
   var e = window.event;
   alert(e.clientX + " / " + e.clientY);
}
亣腦蒛氧 2024-10-01 06:20:56

我寻找类似的东西但最终空手而归。
所以我尝试做相反的事情

我们可以识别除浏览器事件之外的所有事件。
请参阅下面的(未经测试)片段。

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)
   });

这对我有用,但仍有一些事件未处理。
我正在寻找那些。
如果有人对它们有想法,请分享。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文