代码之家  ›  专栏  ›  技术社区  ›  andres descalzo

window.open创建对基于名称的

  •  0
  • andres descalzo  · 技术社区  · 14 年前

    这样我就得到了对打开窗口的引用:

    var refWin = window.open("mypage1", "name_mypage");
    

    refWin.close();
    

    var refWin = window.open("mypage2", "name_mypage");
    

    问:如何根据名称引用窗口?我需要先关闭窗口,然后再打开同名窗口。

    执行以下操作:

    var refWin = window.open("about:blank", "name_mypage");
    refWin.close();
    refWin = window.open("mypage2", "name_mypage");
    

    谢谢

    2 回复  |  直到 14 年前
        1
  •  0
  •   Ryan Kinal    14 年前

    根据安德烈斯的评论,以下是他寻求的解决方案:

    refWin.location = "myurl";
    refWin.focus();
    

    这将在窗口中打开“myurl”,并对其进行聚焦。

    或者。

    var refWin = window.open("mypage1", "name_mypage");
    refWin.focus();
    

    如果您没有再次重新加载页面,那么您不应该有任何理由不能这样做:

    var refWin = window.open("mypage1", "name_mypage");
    refWin.close();
    refWin = window.open("mypage1", "name_mypage");
    

    如果你真的关心它,你可以做你自己的“windows”对象:

    var windows = {};
    var refWin = window.open("mypage1", "name_mypage");
    windows[refWin.name] = refWin;
    windows("name_mypage").close();  // close the window
    

        2
  •  0
  •   andres descalzo    14 年前

    Helper.window = new function () {
    
        // Private fields
        var w = window, s = screen, _self = this, whs = {}, isChrome = /chrome/.test(navigator.userAgent.toLowerCase());
    
        // Public Members
        this.focus = function (wh) {
            if (!wh) return;
            if (isChrome) wh.blur();
            wh.focus();
        };
    
        this.windowExists = function (wt) {
            return wt && whs[wt] && (typeof whs[wt]['closed'] != undefined) && !whs[wt].closed;
        };
    
        this.close = function (wt) {
    
            if (typeof whs[wt][close] != undefined) whs[wt].close();
            whs[wt] = null;
    
            return _self;
        };
    
        this.properties = function (wp) {
    
            wp = (wp || 'menubar=yes').toLowerCase();
    
            if (!(/menubar/.test(wp)))
                wp += 'menubar=yes';
    
            if (!(/location/.test(wp)))
                wp += ',location=yes';
    
            if (!(/width/.test(wp)))
                wp += ',width=' + (s.availWidth - 150);
    
            if (!(/height/.test(wp)))
                wp += ',height=' + (s.availHeight - 150);
    
            if (!(/scrollbars/.test(wp)))
                wp += ',scrollbars=yes';
    
            if (!(/resizable/.test(wp)))
                wp += ',resizable=yes';
    
            return wp;
        };
    
        this.open = function (url, wt, wp) {
    
            if (_self.windowExists(wt))
                return _self.close(wt).open(url, wt, wp);
    
            var urlOpen = '';
            if (typeof url == 'string') {
                urlOpen = url;
            } else if (jQuery(url).get(0).tagName.toLowerCase() == 'a') {
                urlOpen = jQuery(url).attr('href');
            } else {
                urlOpen = 'about:blank';
            }
    
            wp = _self.properties(wp);
            wt = wt || "_blank";
    
            var wh = wp ? w.open(urlOpen, wt, wp) : w.open(urlOpen, wt);
    
            if (wh && "_blank" !== wt) {
                whs[wt] = wh;
                _self.focus(wh);
            }
    
            return wh;
        };
    
    };