JavaScript window.open仅在窗口尚不存在时才打开

时间:2022-10-22 15:03:03

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:

我有一个应用程序,可以在单击链接时打开一个新窗口。这会产生一个包含Java applet的页面。我遇到的问题是单击相同的链接会重新加载页面,从而重置Java应用程序。有没有办法陷阱这个?可接受的两种解决方案是:

  1. Allow multiple windows to be opened from the click handler
  2. 允许从单击处理程序打开多个窗口

  3. Ignore subsequent requests if the window is already open
  4. 如果窗口已经打开,请忽略后续请求

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

抱歉作为一个Javascript新手 - 这不是我的主要事情。

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.

感谢您的回复 - 我稍微修改了建议,以便我可以通过该功能打开多个URL。

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

EDIT2:检查在另一个窗口打开的URL时,此代码还有另一个版本

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

6 个解决方案

#1


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

#2


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

您需要执行2次测试... 1检查是否定义了弹出窗口,2检查是否已关闭。

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

#3


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

#4


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

#5


You could check like this:

你可以像这样检查:

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

#6


Try checking:

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

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

#1


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

#2


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

您需要执行2次测试... 1检查是否定义了弹出窗口,2检查是否已关闭。

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

#3


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

#4


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

#5


You could check like this:

你可以像这样检查:

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

#6


Try checking:

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

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