2017-08-30 228 views
0

我有关于浏览器关闭事件的问题..捕捉浏览器关闭事件没有刷新事件

如何捕获浏览器关闭事件,而不刷新事件。

在此先感谢。

+1

window.onbeforeunload =函数(事件){// 做一些 返回NULL; }; – C2486

+1

可能重复[javascript检测浏览器关闭选项卡/关闭浏览器](https://stackoverflow.com/questions/3888902/javascript-detect-browser-close-tab-close-browser) – pritaeas

+0

我已经尝试使用window.onbeforeunload函数这将对时间的关闭和刷新事件起作用,所以它没有工作。 – Rajasekhar

回答

0

使用window.onbeforeunload但当您重新加载浏览器或通过单击链接移动到其他页面时,也会触发此操作。

window.onbeforeunload = function(e) { 
    var dialogText = 'Dialog text here'; 
    e.returnValue = dialogText; 
    return dialogText; 
}; 

注意:window.onbeforeunload将不适用于除Android外的触控设备。

+0

感谢您的重播..但window.onbeforeunload一次申请关闭和刷新事件,所以它不会为刷新关闭事件工作。 – Rajasekhar

+0

就像我上面说的,是的..也没有别的办法。 –

0

试试这个

window.onbeforeunload = function (event) { 
    var message = 'Important: Please click on \'Save\' button to leave this page.'; 
    if (typeof event == 'undefined') { 
     event = window.event; 
    } 
    if (event) { 
     event.returnValue = message; 
    } 
    return message; 
}; 

$(function() { 
    $("a").not('#LogOut').click(function() { 
     window.onbeforeunload = null; 
    }); 
    $(".btn").click(function() { 
     window.onbeforeunload = null; 
}); 
});