1

Here's a distillation of the problem:

If I open a console and enter the following code as a single block:

var existingWin = window.open('', 'footerContent'); 
existingWin.close();
var existingWin = window.open('', 'footerContent')

the first window.open and the window.close calls work, but the second window.open call doesn't work. Even if I delay it to give the window time to close first:

var existingWin = window.open('', 'footerContent'); 
existingWin.close();
setTimeout(function() {var existingWin = window.open('', 'footerContent')}, 3000) 

it doesn't work. But if I execute the first two lines alone:

var existingWin = window.open('', 'footerContent'); 
existingWin.close();

and then separately enter:

var existingWin = window.open('', 'footerContent'); 

it now opens the window. This suggests that the problem is running them in the same block of code.

This is causing me problems in a unit test I'm building. Can anyone help me understand why this happens and if there's a way to work around it? Thanks in advance.

  • In chrome i tried your code i.e. this: var existingWin = window.open('', 'footerContent'); existingWin.close(); setTimeout(function() {var existingWin = window.open('', 'footerContent')}, 3000) it worked fine – takrishna Apr 04 '19 at 17:42
  • takrishna, I just copied and pasted your snippet and it didn't work for me. It does open and close the first window but doesn't open the second one. Is it possible there's some browser issue? It wouldn't seem so since I'm also running it in fully updated Chrome. I'll try other browsers as well. – Pablo Carson Apr 04 '19 at 17:45
  • 2
    Just curious as to why are you opening a window in a unit test, and why would you need to test that it opens, closes and then opens again? Also, what's your environment, browser...? – leosteffen Apr 04 '19 at 17:46
  • it is opening very quickly and then closing it and opening again - perhaps you are not seeing it open and close ? try placing a timeout before closing – takrishna Apr 04 '19 at 17:47
  • leosteffen, I'm sequentially testing several UI elements that open the same child window with different content. I'd have used different names / variables if that would have helped but it turns out that wasn't the problem as I mentioned below. – Pablo Carson Apr 04 '19 at 17:56

3 Answers3

0

Using the word var initialises, or resets, the variable. Just try it without that word, like:

    existingWin = window.open('', 'footerContent');
user6854465
  • 177
  • 6
  • 1
    Thanks for responding. I tried this and it didn't work. I suspect it has to do with placing it in the same block since, as I said, there's no problem executing them separately. – Pablo Carson Apr 04 '19 at 17:41
0

Turns out it was a default popup blocker setting in my Chrome browser. When I changed the setting, it worked as it should. Thanks to takrishna for pointing me in the right direction. But this does mean I'll probably use a different strategy and stay away from child windows.

  • On second thought, the child windows work fine in the interface, just not as part of a single function (which is how the unit test is structured.) So I'll proceed as is and anyone who runs the unit test will be instructed to allow the domain full access. – Pablo Carson Apr 04 '19 at 18:57
-1

To open multiple window you need to specify second name unique like below.

var existingWin = window.open('', 'footerContent'); 

var existingWin = window.open('', 'footerContent1');

var existingWin = window.open('', 'footerContent2');

You can check the details here https://javascript.info/popup-windows

Obaidul Kader
  • 217
  • 1
  • 7
  • Why do you need to do that? OP is closing the first window and reusing the same name – pushkin Apr 04 '19 at 17:41
  • Thanks for responding. I tried this and it didn't work. I suspect it has to do with placing it in the same block since, as I said, there's no problem executing them separately. – Pablo Carson Apr 04 '19 at 17:42