window对象是JavaScript浏览器对象模型中的顶层对象,包含多个常用方法和属性:
1 打开新窗口
代码如下:
window.open(pageURL,name,parameters)
其中:
pageURL为子窗口路径
name为子窗口句柄
parameters为窗口参数(各参数用逗号分隔)
如:
代码如下:
window.open("http://www.cnblogs.com/zhouhb/","open",'height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');
2 打开模式窗口
代码如下:
window.showModalDialog("http://www.cnblogs.com/zhouhb/","open","toolbars=0;width=200;height=200");
3 关闭窗口,不弹出提示框
如果网页不是通过脚本程序打开的(window.open()),调用window.close()脚本关闭窗口前,必须先将window.opener对象置为null,否则浏览器(IE7、IE8)会弹出一个确定关闭的对话框。
代码如下:
<script language="javaScript">
function closeWindow()
{
window.opener = null;
window.open('', '_self', '');
window.close();
}
</script>
<input type='button' value='关闭窗口' onClick="closeWindow()">
或
<input type="button" value="关闭窗口" onClick="window.opener = null;
window.open('', '_self', '');window.close()">
对于关闭框架窗口
代码如下:
<script language="javaScript">
function closeWindow()
{
window.opener = null;
window.open('', '_top', '');
window.parent.close();
}
</script>
4 location对象使用
代码如下:
window.location.reload();//刷新当前页
window.location.href="http://www.cnblogs.com/zhouhb/"; //载入其他页面
5 history对象使用
代码如下:
window.history.go(1); //前进
window.history.go(-1); //后退
6 子窗体向父窗体传值
6.1 简单方法
(1)在父窗体中打开子窗体
代码如下:
var str=window.showModalDialog("s.html");
if(str!=null)
{
var v=document.getElementById("v");
v.value+=str;
}
(2)子窗体代码
代码如下:
var v=document.getElementById("v");
window.parent.returnValue=v.value;
window.close();
另外,对于showModalDialog打开的窗口,也可以通过dialogArguments传值:
父窗口代码:
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function opendialog()
{
window.showModalDialog("child.html",window,"win","resable=false");//这里用window对象作为参数传递
}
</script>
</head>
<body>
<form>