您可以使用以下内容
<!-- HTML -->
<a id="opener">Open window</a>
// JavaScript
var a = document.getElementById('opener'), w;
a.onclick = function() {
if (!w || w.closed) {
w = window.open("https://www.google.com","_blank","menubar = 0, scrollbars = 0");
} else {
console.log('window is already opened');
}
w.focus();
};
工作jsBin | 更多关于 window.open 方法
如果您想控制多个窗口,请使用下面的代码段
<!-- HTML -->
<a href="https://www.google.com" class="opener">Open google.com</a> |
<a href="http://www.yahoo.com" class="opener">Open yahoo.com</a>
//JavaScript
window.onload = function(){
var a = document.querySelectorAll('.opener'), w = [], url, random, i;
for(i = 0; i < a.length; i++){
(function(i){
a[i].onclick = function(e) {
if (!w[i] || w[i].closed) {
url = this.href;
random = Math.floor((Math.random() * 100) + 1);
w[i] = window.open(url, "_blank", random, "menubar = 0, scrollbars = 0");
} else {
console.log('window ' + url + ' is already opened');
}
e.preventDefault();
w[i].focus();
};
})(i);
}
};
工作jsBin
如果您不希望它们在单独的窗口中加载,只需排除此行
random = Math.floor((Math.random()*100)+1);
random
并从下一行删除引用
w[i] = window.open(url, "_blank", random, "menubar=0,scrollbars=0");
旁注:正如您在上面看到的,我们创建了两个包含一些第三方内容的窗口;您应该知道没有办法从他们那里获得任何参考(对父/打开器窗口)。