如何识别onbeforeunload是由单击关闭按钮引起的 [英] how to identify onbeforeunload was caused by clicking close button

查看:85
本文介绍了如何识别onbeforeunload是由单击关闭按钮引起的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过单击关闭按钮或页面刷新或通常导致onbeforeunload导致的事件来确定是否导致onbeforeunload?

How do I determine if onbeforeunload was caused by clicking the close button or a page refresh or generally what event caused onbeforeunload?

这里是一个片段:

window.onbeforeunload = function( e ){
    if( !e ) e = window.event;

    // insert code to determine which is the source of the event
}

请帮助我。

推荐答案

参考各种文章,做一些试验和错误,最后发展出这个想法对我来说完美无缺,就像我希望它发生的那样。逻辑很简单它也实现了。想法是检测由关闭浏览器触发的卸载事件。在这种情况下,鼠标将离开窗口,指向关闭('X')按钮。

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

ConfirmLeave函数会给出弹出的默认消息,如果有的话需要自定义消息,返回要显示的文本而不是空字符串

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是由单击关闭按钮引起的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆