开发者

window.open create reference to the name based

开发者 https://www.devze.com 2023-01-06 07:25 出处:网络
This way I get the reference to the open window: var refWin = window.open(\"mypage1\", \"name_mypage\");

This way I get the reference to the open window:

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

if you want to close the window, I close with:

refWin.close();

if I do a screen refresh (F5) and 开发者_开发百科run the same line, it opens in the same window, because I put the same name.

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

but with different reference (refWin).

Question: how I can make reference to the window based on the name?, I need to close the window before opening, with the same name.

do something like:

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

but without having to be a blank window for reference.

thanks


Based on andres's comment, here is the solution he's looking for:

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

This will open "myurl" in the window, and focus it.

Or.

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

Edit:

If you're not reloading the page again, there shouldn't be any reason you can't just do this:

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

If you're really concerned about it, you could make your own "windows" object:

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

I don't think that's terribly productive, as you still can't focus it reliably, but maybe it fits your uses.


my solution based on another script:

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;
    };

};  
0

精彩评论

暂无评论...
验证码 换一张
取 消