views:

903

answers:

2

I have a page that creates a popup window, then in the new window i created a new button element and attach a onclick event then attach it to the parent window.

var openerDoc = window.opener.document;
var newButton = openerDoc.createElement('button');
newButton.onclick = function(){
  window.opener.fn();
  return false;
}
var anElementOfParent = openerDoc.getElementById('anElementOfParentWindow');
anElementOfParent.parentNode.insertBefore(newButton,anElementOfParent);

Inside the onclick event of the new button i called a function from the parent window and it works until you close the new window.

A: 

If I have understood you correctly, you are assigning a function defined in the scope of the new window to an element in the opener window. When you close the new window this function will disappear, as it was a property of the new window.

Try defining a function in the opener that creates the button and adds the event handler, then call that function from the new window.

NickFitz
A: 

Thank you very much, now its working :)