码迷,mamicode.com
首页 > Windows程序 > 详细

使用window.open切换到之前打开过的新页面

时间:2019-03-18 11:46:56      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:rem   pen   break   ceo   window   array   false   indexof   ISE   

使用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切换到之前打开过的新页面

标签:rem   pen   break   ceo   window   array   false   indexof   ISE   

原文地址:https://blog.51cto.com/beetsuan/2364522

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!