简体   繁体   English

通过window.open打开时检测窗口关闭事件

[英]detecting window close event when opened through window.open

I am opening a window through window.open method. 我正在通过window.open方法打开一个窗口。 This window is for conducting test. 该窗口用于进行测试。 Can i know how to detect the event when user closes the popup window. 我可以知道当用户关闭弹出窗口时如何检测事件。 I want to detect this in popup window only not parent window.I mean kind of ajax request to insert a value in database when user closes it. 我只想在弹出窗口中而不是父窗口中检测到它。我的意思是一种ajax请求,当用户关闭它时在数据库中插入一个值。 I have tried onbeforeunload but it does not works. 我已经尝试过onbeforeunload但是它不起作用。 Please help me 请帮我

Pretty simple try this 很简单尝试一下

$(window).on('beforeunload', function() { 
return 'Are you sure want to logout ?'; 
});

$(window).unload(function() { 
debugger;    // when closing window debugger will hit here
});

If you want to know when a window or tab is closed then use unload or onbeforeunload , these events are fired too, when you leave the site by a link. 如果您想知道何时关闭窗口或选项卡,请使用unloadonbeforeunload ,当您通过链接离开站点时,也会触发这些事件。

The API for jQuery unload() : https://api.jquery.com/unload/ jQuery unload()的API: https : //api.jquery.com/unload/

...and pure javascript: https://developer.mozilla.org/de/docs/Web/API/WindowEventHandlers/onbeforeunload ...和纯JavaScript: https//developer.mozilla.org/de/docs/Web/API/WindowEventHandlers/onbeforeunload

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM