如何捕获浏览器窗口的clsoing事件?

时间:2015-11-18 08:05:32

标签: javascript jquery browser

您好我想捕获关闭窗口的事件,并希望删除会话值。经过几次搜索后,我发现onBeforeUnload事件,每当你导航到不同页面或卸载页面时触发事件。重新加载时,将触发此事件,这不是我的要求。我希望所有浏览器中只有特定的窗口关闭事件才能使会话值无效。

1 个答案:

答案 0 :(得分:-1)

Run JavaScript code on window close or page refresh?

/* The most acceptable answer seemed to be as follows in vanilla JS 
   and was addressed at the linked address already.*/    
window.onbeforeunload = closingCode;
function closingCode(){
   //I would capture the URL prior to unload here and if it does NOT
   //exist or is not your url domain then you can unload variables here 
   return false;

}