1、一般方法(存在刷新问题)
a.htm (父窗口)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>父窗口</title>
<script language="javascript">
var wind; //全局变量
function winopen()
{
wind=window.open("b.htm","childwind","",""); //打开子窗口
}
function winclose()
{
if(typeof(wind)!="undefined"&&wind.open&&!wind.closed) //判断是否存在子窗口并处于打开状态
{
wind.close(); //关闭子窗口
}
}
</script>
</head>
<body>
<input type="button" value="打开子窗口" id="btn1" οnclick="winopen();">
<input type="button" value="关闭子窗口" id="btn2" οnclick="winclose();">
</body>
</html>
存在问题:当父窗口进行刷新时,wind将等于undefined,所以无法找到子窗口并将其关闭
2、我的方法(解决刷新问题)
a.htm (父窗口)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>父窗口</title>
<script language="javascript">
var wind; //全局变量
function winopen()
{
wind=window.open("b.htm","childwind","",""); //打开子窗口,注意链接地址为:b.htm你要打开的页面
}
function winclose()
{
wind=window.open("b.htm","childwind","",""); //打开子窗口,注意窗口名与上面的一样:childwind,链接地址为:c.htm用于处理关闭
}
</script>
</head>
<body>
<input type="button" value="打开子窗口" id="btn1" οnclick="winopen();">
<input type="button" value="关闭子窗口" id="btn2" οnclick="winclose();">
</body>
</html>
c.htm (处理关闭的页面)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新建网页 1</title>
<script language="javascript">
function winopen()
{
window.close(); //将自身关闭
}
</script>
</head>
<body οnlοad="winopen();">
</body>
</html>