0

This is my code. When I click the close button on the child window it will display suresh on the screen but if I call popuponclick() function at the time itself suresh is getting displayed. What I do??

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;
};
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Suresh
  • 31
  • 8
  • 1
    what is your actual requirement? – Ameerudheen.K Jul 15 '19 at 05:00
  • Welcome to stackoverflow. Please explain what you want to happen, and what is actually happening at the moment? – Ahmad Jul 15 '19 at 05:17
  • 1
    Remove the () from `OnChildWindowClose()` - when you call it with `OnChildWindowClose()` it executes immediately. Your statement should be `attachEvent("onunload", OnChildWindowClose);` – mplungjan Jul 15 '19 at 05:21
  • 1
    _This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers_ – mplungjan Jul 15 '19 at 05:21
  • Also attachEvent is IE<9, addEventListener is more supported – mplungjan Jul 15 '19 at 05:25
  • When I click the child window close button after that "Suresh" should get displayed on screen @AmeerPappay – Suresh Jul 15 '19 at 05:28
  • @mplungjan It's not working for me. If I remove the () it's getting "(index):47 Uncaught TypeError: window.ChildWindow.attachEvent is not a function" this error. – Suresh Jul 15 '19 at 05:33
  • 1. Why use attachEvent in the first place? It is an IE only construct only needed for IE<9. 2. Use addEventListener instead https://stackoverflow.com/questions/2657182/correct-usage-of-addeventlistener-attachevent – mplungjan Jul 15 '19 at 05:38
  • Thanks, **attachEvent** worked in IE. But addEventListener not worked in chrome. Why? What do I need to do? @mplungjan – Suresh Jul 15 '19 at 06:04

1 Answers1

0

This will likely work better on your server

It does not run if there is a popup blocker, then you will need to test if ChildWindow was opened correctly

this fiddle works, but stacksnippets fail on the popup blocking

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>
mplungjan
  • 169,008
  • 28
  • 173
  • 236