JavaScript window.open only if the window does not already exist
Asked Answered
K

6

21

I have an application that opens a new window on clicking a link. This spawns a page that holds a Java applet. The problem I am having is that clicking the same link reloads the page, which resets the Java application. Is there any way to trap this? Two solutions that would be acceptable are:

  1. Allow multiple windows to be opened from the click handler
  2. Ignore subsequent requests if the window is already open

Apologies for being a Javascript newbie - it's not really my main thing.

The code attached to the handler is

function launchApplication(l_url, l_windowName)
{
  var l_width = screen.availWidth;
  var l_height = screen.availHeight;

  var l_params = 'status=1' +
                 ',resizable=1' +
                 ',scrollbars=1' +
                 ',width=' + l_width +
                 ',height=' + l_height +
                 ',left=0' +
                 ',top=0';

  winRef = window.open(l_url, l_windowName, l_params);
  winRef.moveTo(0,0);
  winRef.resizeTo(l_width, l_height);
}

EDIT:

Thanks for the replies - I modified the suggestions slightly so that I could have more than one URL opened via the function.

EDIT2: There is another version of this code at Check for a URL open on another window

var g_urlarray = [];

Array.prototype.has = function(value) {
    var i;
    for (var i in this) {
        if (i === value) {
            return true;
        }
    }
    return false;
};


function launchApplication(l_url, l_windowName)
{
  var l_width = screen.availWidth;
  var l_height = screen.availHeight;
  var winRef;

  var l_params = 'status=1' +
                 ',resizable=1' +
                 ',scrollbars=1' +
                 ',width=' + l_width +
                 ',height=' + l_height +
                 ',left=0' +
         ',top=0';
  if (g_urlarray.has(l_url)) {
    winRef = g_urlarray[l_url];
  }
  alert(winRef);
  if (winRef == null || winRef.closed) {
      winRef = window.open(l_url, l_windowName, l_params);
      winRef.moveTo(0,0);
      winRef.resizeTo(l_width, l_height);
      g_urlarray[l_url] = winRef;
  }
}
Kibler answered 9/2, 2009 at 15:42 Comment(0)
H
20

I'd do it like this - basically store all the referenced opened windows on the function itself. When the function fires, check if the window doesn't exist or has been close - of so, launch the popup. Otherwise, focus on the existing popup window for that request.

function launchApplication(l_url, l_windowName)
{
  if ( typeof launchApplication.winRefs == 'undefined' )
  {
    launchApplication.winRefs = {};
  }
  if ( typeof launchApplication.winRefs[l_windowName] == 'undefined' || launchApplication.winRefs[l_windowName].closed )
  {
    var l_width = screen.availWidth;
    var l_height = screen.availHeight;

    var l_params = 'status=1' +
                   ',resizable=1' +
                   ',scrollbars=1' +
                   ',width=' + l_width +
                   ',height=' + l_height +
                   ',left=0' +
                   ',top=0';

    launchApplication.winRefs[l_windowName] = window.open(l_url, l_windowName, l_params);
    launchApplication.winRefs[l_windowName].moveTo(0,0);
    launchApplication.winRefs[l_windowName].resizeTo(l_width, l_height);
  } else {
    launchApplication.winRefs[l_windowName].focus()
  }
}
Hispaniola answered 9/2, 2009 at 15:56 Comment(2)
yep, that's how I'd do it - lower footprint, more reuse, self contained +1Midwifery
+1 works great. One thing though, you have winRefs in the original check but then you misspell it later as winrefsOverfill
G
20

You need to perform 2 tests... 1 check if the popup window is defined, and 2 check if it was closed.

if(typeof(winRef) == 'undefined' || winRef.closed){
  //create new
  winRef = window.open(....);
} else {
  //it exists, load new content (if necs.)
  winRef.location.href = 'your new url';
  //give it focus (in case it got burried)
  winRef.focus();
}
Gallivant answered 9/2, 2009 at 15:59 Comment(3)
Excellent Solution as cross browserConsonance
But it reloads the window even if the URL is sameConsonance
@user2826057 as per the note in my comment in the code - if you don't want to reload the content if it is the same URL you can test if(winRef.location.href != myNewURL){...} to only reload if different.Gallivant
A
5

You can use something like this in the page that opens the new window:

var newWindow = null;

function launchApplication()
{
  // open the new window only if newWindow is null (not opened yet)
  // or if it was closed
  if ((newWindow == null) || (newWindow.closed))
    newWindow = window.open(...);
}
Ammoniacal answered 9/2, 2009 at 15:45 Comment(2)
Or simply: if (!newWindow || newWindow.closed)Cuyler
this does not focus the window if already opened and also never load with the new URLConsonance
K
3

Working code

var newwindow = null;
function popitup(url) {
    if ((newwindow == null) || (newwindow.closed)) {
        newwindow=window.open(url,'Buy','width=950,height=650,scrollbars=yes,resizable=yes');
        newwindow.focus();
    } else {
        newwindow.location.href = url;
        newwindow.focus();    
    }
}  
Knightly answered 6/5, 2012 at 1:21 Comment(0)
C
2

You could check like this:

if(!winref || winref.closed)
{
}
Cambrai answered 9/2, 2009 at 15:46 Comment(1)
@Midwifery -- I think there's no null-pointer problem, because if !winref is true, then the second part of the IOR -- winref.closed -- should never be checked. Does that seem right to you? Or am I dead wrong?Improve
O
0

Try checking:

if (!winref || winref.closed || !winref.document) { }

Oleta answered 5/11, 2011 at 2:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.