子窗口关闭按钮

时间:2019-07-15 04:55:56

标签: javascript

这是我的代码。当我单击子窗口上的关闭按钮时,它将在屏幕上显示suresh,但是如果我在显示suresh时调用 popuponclick()函数。我做什么?

popuponclick = function() {
  window.ChildWindow = window.open('GOLF12/shared/launchpage.html',
    'popupWindow',
    'width=700,height=700');
  window.ChildWindow.attachEvent("onunload", OnChildWindowClose());
}
OnChildWindowClose = function() {
  document.getElementById("my").innerHTML = "suresh";
  window.ChildWindow = null;
};

1 个答案:

答案 0 :(得分:0)

这可能会在您的服务器上更好地工作

如果有弹出窗口阻止程序,它将无法运行,那么您将需要测试ChildWindow是否已正确打开

fiddle works,但stacksnippets在弹出窗口阻止时失败

var ChildWindow;
function  popuponclick() {
  ChildWindow = window.open('GOLF12/shared/launchpage.html',
    'popupWindow',
    'width=700,height=700');
  setTimeout(function() { // we need to have the new page active
    ChildWindow.addEventListener("unload",OnChildWindowClose)
  },100)
}
function OnChildWindowClose() {
  this.opener.document.getElementById("my").innerHTML = "suresh";
  this.opener.ChildWindow = null;
};
<button onclick="popuponclick()">Click</button>
<span id="my"></span>