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

前端 未结 6 1742
一个人的身影
一个人的身影 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:03

    In JavaScript, you can only gain references to the current window and any windows that you open with window.open.

    You could check for winRef.closed to see if the user closed the window, though. I'm not sure if this works well on all browsers or not, though.

    0 讨论(0)
  • 2021-01-01 03:08

    @annakata (and even if you stored them, you wouldn't have permission to close them any more)

    Not true. If you have the name of the window, you can use window.open to reestablish a link to the window even if the opener was closed and reopened. For example:

    <script>
    function winOpen(url){
      return window.open(url,getWinName(url));
    }
    function winClose(url){
      var win = window.open("",getWinName(url));
      win.close();
    }
    function getWinName(url){
      return "win" + url.replace(/[^A-Za-z0-9\-\_]*/g,"");
    }
    </script>
    <a href="#" onclick="winOpen('http://google.com');return false;">Click me first</a>, close and open this window, then
    <a href="#" onclick="winClose('http://google.com');return false;">click me to close the other window</a>
    
    0 讨论(0)
  • 2021-01-01 03:10

    No, this would be a security/privacy issue.


    Since others have brought up the ownership/cookie state storage: this only works if you are also the same document which opened the window, i.e. in the scenario where the user shuts the window down and reopens then these references are indeed lost (and even if you stored them, you wouldn't have permission to close them any more)

    0 讨论(0)
  • 2021-01-01 03:17

    You could actually do it with cookies but... if you ask me, you won't do it.

    0 讨论(0)
  • 2021-01-01 03:23

    If you gave each window a unique window name (the second argument of window.open), calling window.open again with the same window name will either open the window if it's closed, or return a reference to the existing window without opening a new window.

    0 讨论(0)
  • 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...

    0 讨论(0)
提交回复
热议问题