如何确定 onbeforeunload 是否是由单击关闭按钮或页面刷新引起的,或者通常是什么事件导致 onbeforeunload?
这是一个片段:
window.onbeforeunload = function( e ){
if( !e ) e = window.event;
// insert code to determine which is the source of the event
}
请帮我。
如何确定 onbeforeunload 是否是由单击关闭按钮或页面刷新引起的,或者通常是什么事件导致 onbeforeunload?
这是一个片段:
window.onbeforeunload = function( e ){
if( !e ) e = window.event;
// insert code to determine which is the source of the event
}
请帮我。
参考各种文章并进行了一些试验和错误,最终开发了这个想法,它对我来说非常完美,就像我希望它发生的方式一样。逻辑也很简单,它实现了这个想法是检测关闭浏览器触发的卸载事件。在这种情况下,鼠标将离开窗口,指向关闭('X')按钮。
$(window).on('mouseover', (function () {
window.onbeforeunload = null;
}));
$(window).on('mouseout', (function () {
window.onbeforeunload = ConfirmLeave;
}));
function ConfirmLeave() {
return "";
}
ConfirmLeave 函数会给出弹出的默认消息,如果需要自定义消息,返回要显示的文本而不是空字符串
看看这是否有帮助,:)
据我所知,没有办法确定导致 onbeforeunload
. 该事件在窗口即将关闭时触发,无论是关闭浏览器还是其他方式。
如果按下关闭按钮,则值为e.clientY
负。对于其他可能的来源,我怀疑是否有解决方案。
window.onbeforeunload = function() {
var e = window.event;
alert(e.clientX + " / " + e.clientY);
}
我搜索了类似的东西,但最终空手而归。所以我试着做相反的事情
我们可以识别除浏览器事件之外的所有事件。请参阅下面的(未经测试的)片段。
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)
});
这对我有用,但仍有一些事件未处理。我正在寻找那些。如果有人对他们有想法,请分享。