jQuery window.onclose 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15769514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
window.onclose function
提问by user937194
I use this function to call on my window close.
我使用这个函数来调用我的窗口关闭。
This is the confirmation box popup window.
这是确认框弹出窗口。
if(confirm("Sure you want to close the window");
{
// yes return to submit function
}
else
{
// no return to Other call function
}
window.onclose = function()
{
alert('yes');
}
On Close of window on the top right corner with X symbol I need to return false. I am trying to use this window.onclose
function but its not poping up.
在右上角带有 X 符号的窗口关闭时,我需要返回 false。我正在尝试使用此window.onclose
功能,但没有弹出。
Can anybody help me out?
有人可以帮我吗?
回答by Prakash Chennupati
Unfortunately the popup window does not have any close event that you can listen to but there is a closed property that is true when window gets closed. A solution to get around this problem is to start a timer and check the closed property of the child window every second and clear the timer when the window gets closed. Here is the code:
不幸的是,弹出窗口没有任何可以收听的关闭事件,但是当窗口关闭时有一个关闭的属性为真。解决这个问题的一个解决方案是启动一个计时器并每秒检查子窗口的关闭属性,并在窗口关闭时清除计时器。这是代码:
var win = window.open('http://www.google.com','google','width=800,height=600,status=0,toolbar=0');
var timer = setInterval(function() {
if(win.closed) {
clearInterval(timer);
alert('closed');
}
}, 1000);
回答by Denys Séguret
There is no "close" event that you can catch with today's browsers.
当今的浏览器无法捕捉到“关闭”事件。
There is an onbeforeunloadbut you can't do a lot when it is called, especially you can't prevent the window closing without the user consent and most distant operations will fail if you try them from the page which is being closed.
有一个onbeforeunload但是当它被调用时你不能做很多事情,特别是你不能在未经用户同意的情况下阻止窗口关闭,如果你从正在关闭的页面尝试它们,大多数远程操作都会失败。
For a popup window, you can get the closing event, and do long operations, but only in the opener window :
对于弹出窗口,您可以获得关闭事件,并进行长时间的操作,但只能在打开器窗口中:
var w = window.open('popup.html');
w.onbeforeunload = function(){
// set warning message
};
IMPORTANT: In recent versions of chrome, onbeforeunload
only allows you to set the warning message; you may not run extra logic.
重要提示:在最新版本的 chrome 中,onbeforeunload
只允许您设置警告消息;您可能不会运行额外的逻辑。