Joey Thomas
Joey Thomas

Reputation: 113

window.close current window when another window.open is requested

Thank you for taking the time to look at my question.

I have two different window.open requests depending on if a user clicks on either a mobile button or a webcam button.

These windows will open fine and in the correct size, however if the webcam button is clicked while the (smaller) mobcamWindow is open, I want the current mobcamWindow to close and the new (larger) webcamWindow to open (in its correct size) and vice versa.

Currently, the window does not close, but the URL will change to the correct mobcamWindow & webcamWindow respectively, however the window will remain the same size.

I have tried to do window.resize which did not work, and window.close does not work either.

My code is as follows. (I have two functions to handle each mobcamWindow & webcamWindow, with an IF statement in each to check if mobcamWindow or webcamWindow is open, and to close it if it is.

var webcamWindow = null; // global variable
var mobcamWindow = null; // global variable

function openStreamPopupWebcam(elem) {

  if(webcamWindow == null || webcamWindow.closed)
  /* if the pointer to the window object in memory does not exist
     or if such pointer exists but the window was closed */

  { 

    if(mobcamWindow){
      mobcamWindow.close();
    }
    /*if mobile window is open, close mobile window*/

    webcamWindow = window.open(elem.href, "window", "width=470,height=320,resizable,status").focus();
    /* then create it. The new window will be created and
       will be brought on top of any other window. */

  }

}

function openStreamPopupMobile(elem) {

  if(mobcamWindow == null || mobcamWindow.closed)
  /* if the pointer to the window object in memory does not exist
     or if such pointer exists but the window was closed */

  { 

    if(webcamWindow){
      webcamWindow.close();
    }
    /*if mobile window is open, close mobile window*/

    mobcamWindow = window.open(elem.href, "window", "width=445,height=666,resizable,status").focus();
    /* then create it. The new window will be created and
       will be brought on top of any other window. */

  }

}

If you need any more information or are finding it hard to understand me please ask and I will try and explain as best I can.

Thank you.

Upvotes: 1

Views: 145

Answers (2)

Joey Thomas
Joey Thomas

Reputation: 113

Answered by Teemu

Give different names for your windows, now both are named as window. – Teemu

Thank you.

Also thank you Adarsh Mohan for pointing out that the values should be reinitialized to 'null' on close.

Upvotes: 0

Adarsh Mohan
Adarsh Mohan

Reputation: 1384

You have to reinitialize the values to 'null' when those conditions are satisfied

var webcamWindow = null; // global variable
var mobcamWindow = null; // global variable
function openStreamPopupWebcam(elem){
  if(webcamWindow == null || webcamWindow.closed){
    if(mobcamWindow){
      mobcamWindow.close();
      mobcamWindow = null;
    }
    webcamWindow = window.open(elem.href, "window", "width=470,height=320,resizable,status").focus();
  }
}
function openStreamPopupMobile(elem){
  if(mobcamWindow == null || mobcamWindow.closed){
    if(webcamWindow){
      webcamWindow.close();
      webcamWindow = null;
    }
    mobcamWindow = window.open(elem.href, "window", "width=445,height=666,resizable,status").focus();
  }
}

Upvotes: 1

Related Questions