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

前端 未结 6 2222
半阙折子戏
半阙折子戏 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();
    }
    

提交回复
热议问题