日期:2014-05-16  浏览次数:20382 次

JavaScript窗口之间值传递—window.open打开窗口之间传值

window.open打开窗口之间传值

父窗口代码

<!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 language="javascript">	
	var win;
	function test_open() {
		// 打开一个新窗口,返回一个新窗口的引用
		win = window.open("son.html", "win", "width:200, height:200");
	}
	
	// 父窗口对子窗口赋值
	function test_set() {
		// 通过名字给表单元素赋值
		win.document.getElementsByName("age")[0].value = "20";
		var str = "给子窗口赋值成功,值为:";
		alert(str + win.document.getElementsByName("age")[0].value);
	}
</script>
</head>

<body>
	用户名:<input type="text" name="username" /><br />
    <input type="button" onclick="test_open()" value="打开一个新窗口" /><br />
    <input type="button" onclick="test_set()" value="给子窗口赋值" /><br />
</body>
</html>

?

?

子窗口代码

<!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 language="javascript">	
	// 子窗口给父窗口赋值		
	function test_parent() {
		// 通过opener指向父窗口(可以访问父窗口的所有对象)
		window.opener.document.getElementsByName("username")[0].value = "keveon";
		var str = "给父窗口赋值成功,值为:";
		alert(str + window.opener.document.getElementsByName("username")[0].value);
	}
</script>
</head>
<body>
	年龄:<input type="text" name="age" /><br />    
    <input type="button" value="给父窗口赋值" onclick="test_parent()" />    
</body>
</html>

?