個人思路是:把已經打開過的頁面的title和window對象存起來,若是要打開的頁面的title已經存在了,就直接foucs到已存在的頁面window對象,不新調用open函數。html
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; };