How can I check for an open URL in another window?

前端 未结 6 1794
一个人的身影
一个人的身影 2021-01-01 03:03

This is a follow up to my last question Open a window if the window does not already exist Essentially, I am now keeping a list of all the window references that have been o

6条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-01 03:30

    Setup an array, and increment it with window references when you open them...

    var wins = new Array();
    
    function openWindow(url) {
      wins.push(window.open(url));
    }
    

    Then when you wish to check the status of the windows, you can loop through them like this, and remove the windows that are not opened...

    function updateWindowArray() {
      for(var i = 0, l = wins.length; i < l; i++) {
        if(wins[i] == null || wins[i].closed)
          arrayRemove(wins, i, i + 1);
      }
    }
    
    function arrayRemove(array, from, to) {
      var rest = array.slice((to || from) + 1 || array.length);
      array.length = from < 0 ? array.length + from : from;
      return array.push.apply(array, rest);
    }
    

    Best regards...

提交回复
热议问题