<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button onclick="openWindow()">打开新窗口</button>
<button onclick="closeWindow()">关闭当前窗口</button>
</body>
<script type="text/javascript">
var win;
var openWindow = function () {
win = win||[];
//传入参数名字 打开的页面定义一个名字
var name = "baidu";
//目标页面
var target = "http://baidu.com";
//判断是否打开
var flag = false;
for(var i=0;i<win.length;i++){
var objWin = win[i];
if(objWin.name === name){
flag = true;
//已经存在
objWin.win.location.replace(target);
objWin.win.focus();
break;
}
}
if(!flag){
var cWin = window.open(target);
var obj = {
name:'baidu',
win:cWin
}
win.push(obj);
}
}
var closeWindow = function () {
var userAgent = navigator.userAgent;
if (userAgent.indexOf("Firefox") != -1 || userAgent.indexOf("Chrome") !=-1) {
window.location.href="about:blank";
window.close();
history.back();
} else {
window.opener = null;
window.open("", "_self");
window.close();
}
}
</script>
</html>
网友评论