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

前端 未结 6 2191
半阙折子戏
半阙折子戏 2020-11-28 07:48

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

相关标签:
6条回答
  • 2020-11-28 08:08

    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();
    }
    
    0 讨论(0)
  • 2020-11-28 08:17

    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()
      }
    }
    
    0 讨论(0)
  • 2020-11-28 08:18

    Try checking:

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

    0 讨论(0)
  • 2020-11-28 08:20

    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();    
        }
    }  
    
    0 讨论(0)
  • 2020-11-28 08:27

    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(...);
    }
    
    0 讨论(0)
  • 2020-11-28 08:29

    You could check like this:

    if(!winref || winref.closed)
    {
    }
    
    0 讨论(0)
提交回复
热议问题