Javascript实现子窗口向父窗口传值(转)
page1.htm为父窗口,page2.htm为子窗口。详细代码如下:
page1.htm
引用
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<title>子窗口传值给父窗口</title>
<script language="JavaScript" type="text/javascript">
function openWin(u, w, h) {
var l = (screen.width - w) / 2;
var t = (screen.height - h) / 2;
var s = 'width=' + w + ', height=' + h + ', top=' + t + ', left=' + l;
s += ', toolbar=no, scrollbars=no, menubar=no, location=no, resizable=no';
open(u, 'oWin', s);
}
function openIt(){
window.open("page2.htm",400,300);
}
</script>
</head>
<body>
<input type="text" id="text1" />
<input type="button" value="弹出" onclick="openIt()" />
</body>
</html>
page2.htm
引用
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<title>子窗口传值给父窗口</title>
<script language="JavaScript" type="text/javascript">
function goback(obj){
window.opener.document.getElementById("text1").value = obj.value;
window.close();
}
</script>
</head>
<body>
<div><input type="button" value="阿会楠" onclick="goback(this)" /></div>
<div><input type="button" value="23岁" onclick="goback(this)" /></div>
</body>
</html>
page1.htm
引用
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<title>子窗口传值给父窗口</title>
<script language="JavaScript" type="text/javascript">
function openWin(u, w, h) {
var l = (screen.width - w) / 2;
var t = (screen.height - h) / 2;
var s = 'width=' + w + ', height=' + h + ', top=' + t + ', left=' + l;
s += ', toolbar=no, scrollbars=no, menubar=no, location=no, resizable=no';
open(u, 'oWin', s);
}
function openIt(){
window.open("page2.htm",400,300);
}
</script>
</head>
<body>
<input type="text" id="text1" />
<input type="button" value="弹出" onclick="openIt()" />
</body>
</html>
page2.htm
引用
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<title>子窗口传值给父窗口</title>
<script language="JavaScript" type="text/javascript">
function goback(obj){
window.opener.document.getElementById("text1").value = obj.value;
window.close();
}
</script>
</head>
<body>
<div><input type="button" value="阿会楠" onclick="goback(this)" /></div>
<div><input type="button" value="23岁" onclick="goback(this)" /></div>
</body>
</html>