1. 程式人生 > >使用window.open切換到之前打開過的新頁面

使用window.open切換到之前打開過的新頁面

fin turn ret array 數據類型 visible mov ++ prot

使用window.open打開頁面,不刷新已經打開過的頁面

我的思路是:把已經打開過的頁面的title和window對象存起來,如果要打開的頁面的title已經存在了,就直接foucs到已存在的頁面window對象,不新調用open函數。

  var iframeWin = [];
    $("#btn-open-in-new-win").on("click",function () {
        var iframe =  $(".iframe-box:visible iframe");
        if(!iframe.length)
            return;
        var id = $(iframe).contents().find("title").html();
        var win = iframeWin.search(id,[‘id‘],true);
        if(!win.length){
            win = window.open(iframe[0].src,id);
            iframeWin.push({id:id,win:win});
        }
        else if (win[0].win.closed) {
            iframeWin.removeIfExited({id:id},[‘id‘]);
            win = window.open(iframe[0].src,id);
            iframeWin.push({id:id,win:win});
        }else{
            win[0].win.focus();
        }
    });

之前寫的js數組方法

/**
 *在數組中查找值
 * value: 值 或 值數組
 * props: 對象數組的屬性名稱數組,屬性名稱,不可以為null
 * isEquals 是否不是匹配,而是相等
 */
Array.prototype.search = function(value, props, isEquals) {

    var len,results= [];
    if(value && value instanceof  Array){
        var resultsArr = [],resultsTemp;
        len = value.length;
        if(!len){
            return this;
        }
        for (var i = 0; i < len; i++) {
            resultsTemp = this.search(value[i],props,isEquals);
            for (var j = 0; j < resultsTemp.length; j++) {
                resultsArr.push(resultsTemp[j]);
            }
        }
        return resultsArr;
    }

    if (value == false) {
    } else if (null == value || "" == value)
        return this;
    len = this.length;
    var prop;
    for (var i = 0; i < len; i++) {
        if (props) {
            for (var j = 0; j < props.length; j++) {
                prop = this[i][props[j]];
                if (isEquals) {
                    if (prop == value) {
                        if (value == false && prop + "" == "") {
                            continue;
                        }
                        results.push(this[i]);
                        break;
                    }
                } else if (prop && (prop + "").indexOf(value) != -1) {
                    results.push(this[i]);
                    break;
                }
            }
        } else {
            for ( var j in this[i]) {
                if (j && (j + "").indexOf(value) != -1) {
                    results.push(this[i]);
                    break;
                }
            }
        }
    }
    return results;
};

/**
 *在數組中移除值
 * obj: 值或對象
 * prop: 對象數組的屬性名稱 用作判斷數組中是否存在屬性為prop的obj對象;可以為null,為null是認為數組obj為基本數據類型
 */
Array.prototype.removeIfExited = function(obj, prop) {
    var len = this.length;
    for (var i = 0; i < len; i++) {
        if (prop && (this[i][prop] === obj[prop])) {
            this.splice(i, 1);
            return true;
        } else if (this[i] === obj) {
            this.splice(i, 1);
            return true;
        }
    }
    return false;
};

使用window.open切換到之前打開過的新頁面