Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

popup - JavaScript window.open only if the window does not already exist

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;
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...