Window.open only if the window is not open

前端 未结 3 1579
予麋鹿
予麋鹿 2021-01-18 02:14

I have a link on my site that opens a new window to a page that plays a very long audio file. My current script works fine to open the page and not refresh if the link is cl

3条回答
  •  长情又很酷
    2021-01-18 02:38

    You should first to call winRef = window.open("", "winPopup") without URL - this will return a window, if it exists, without reloading. And only if winRef is null or empty window, then create new window.

    Here is my test code:

    var winRef;
    
    function OpenWindow()
    {
      if(typeof(winRef) == 'undefined' || winRef.closed)
      {
        //create new
        var url = 'http://someurl';
        winRef = window.open('', 'winPop', 'sampleListOfOptions');
        if(winRef == null || winRef.document.location.href != url)
        {
          winRef = window.open(url, 'winPop');
        }
      }
      else
      {
        //give it focus (in case it got burried)
        winRef.focus();
      } 
    }
    

    It works.

提交回复
热议问题