简体   繁体   English

如何将关闭事件绑定到window.open()

[英]how to bind an close event to window.open()

I have the following function to open a url in a window pop up: 我具有以下功能,可在弹出的窗口中打开网址:

function windowPopup(url, width, height) {
  // center 
  var left = (screen.width / 2) - (width / 2),
      top = (screen.height / 2) - (height / 2);

  handle=window.open(
    url,
    "",
    "menubar=no,toolbar=no,resizable=yes,scrollbars=yes,width=" + 
    width + ",height=" + height + ",top=" + top + ",left=" + left
  );
}

I want to be able to detect when this window is closed an execute some code. 我希望能够检测到何时关闭此窗口并执行一些代码。 How can I use the handle in the function to bind an unload or close? 如何使用函数中的句柄绑定卸载或关闭?

I want to know once the window has been closed after someone shares to twitter: 我想知道有人分享到Twitter后窗口是否关闭:

windowPopup("https://twitter.com/intent/tweet/?text=Check%20out%20this%20website!&url=https%3A%2F%2Fgoogle.com%2F", 500, 300);

Update 更新资料

I updated with Lar's suggestion: 我更新了Lar的建议:

function windowPopup(url, width, height) {
  // center 
  var left = (screen.width / 2) - (width / 2),
      top = (screen.height / 2) - (height / 2);



  handle=window.open(
    url,
    "",
    "menubar=no,toolbar=no,resizable=yes,scrollbars=yes,width=" + 
    width + ",height=" + height + ",top=" + top + ",left=" + left
  );

  handle.onbeforeunload = function() {
    alert('Bye!');
  }

}

While this worked for some pop up windows it didn't work when i closed the twitter window... 虽然这适用于某些弹出窗口,但当我关闭Twitter窗口时却不起作用...

You're looking for the beforeunload event: 您正在寻找beforeunload事件:

handle.onbeforeunload = function() {
  console.log('Bye!');
}

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

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