0

I've difficult closing popup. window.close(); is working only with Chrome not IE. After doing some researches, I found this article which tells why I shouldn't use window.close() directly in the popup to close it.

//Global var to store a reference to the opened window
var openedWindow;

function openWindow() {
  openedWindow = window.open('moreinfo.htm');
}

function closeOpenedWindow() {
    openedWindow.close();
}

function myReload() {
   location.reload();
}

The problem is that the first time I wrote the closeOpenWindow method, I forgot to put the () at the end, so it was openedWindow.close;

I've fixed the problem, but I'm still getting the same error on that line.

openedWindow.close is not a function

If I delete the the entire function closeOpenedWindow(), I get the error that closeOpenedWindow() is not a function. When I put it back, I get the previous error that openedWindow.close is not a function.

Thanks for helping

EDIT:

This is how I'm calling the function from the popupu:

function mycloseChild() {
  window.opener.myReload();
  window.opener.closeOpenedWindow();
}
Richard77
  • 20,343
  • 46
  • 150
  • 252

2 Answers2

0

Actually, this seems to work in Chrome Debugger. Maybe a timing issue?

//Global var to store a reference to the opened window
var openedWindow;

function openWindow() {
  openedWindow = window.open('moreinfo.htm');
}

function closeOpenedWindow() {
    openedWindow.close();
}

openWindow();
//openedWindow is new window
closeOpenedWindow();
//openedWindow closes

It looks like you can use window.opener from the openedWindow to get a reference to the original window object which has the method closeOpenedWindow. but in order for that to work, you need to add closeOpenedWindow to the original window like so

window['closeOpenedWindow'] = function() {
    openedWindow.close();
}

and then call it from the opened window like so window.opener.closeOpenedWindow()

see also Can I pass a JavaScript variable to another browser window?

Community
  • 1
  • 1
4m1r
  • 12,234
  • 9
  • 46
  • 58
  • `OpenWindow` and `CloseOpenedWindow` are defined in the parent page. `CloseOpenWindow` is being called from the `popup`. – Richard77 Oct 27 '16 at 20:47
  • well, that's your problem then, no? The popup is a new window with no reference to openedWindow. see updated answer – 4m1r Oct 27 '16 at 21:01
  • I think so, because when I run `console.log(openedWindow);` from the popup, this is the response I'me getting: `Uncaught ReferenceError:: openedWindow is not defined at ` – Richard77 Oct 27 '16 at 21:06
  • if you attach the function to the opener as described above, you should be able to `window.opener.closeOpenedWindow()` granted in same security domain. – 4m1r Oct 27 '16 at 21:09
  • I've made the modification you mentioned above. I even change the name of the window from `openedWindow` to `popup`, It still complains that `popup is not defined`. – Richard77 Oct 27 '16 at 21:22
0
var openedWindow = {
    open: () => { window.open('moreinfo.htm') },
    close: () => { window.close() }
}
richdotjs
  • 204
  • 1
  • 2
  • 10