利用 JavaScript 中的 window.open()函数打开一个页面的时候,传值有什么好的方法,以下是代码,主要是利用 window.opener这个对象来完成的。经测试在 IE8 Firefox 火狐下测试通过
1.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">
//弹出窗口 Js 函数
function openPage(){
window.open('2.html');
}
//弹出模式对话框 Js 函数
function openMode()
{
window.showModalDialog('3.html');
}
</script>
</head>
<body>
<input id="reText" />
<input type="button" value="弹出窗口" onclick="openPage();" />
<input type="button" value="弹出模式对话框" onclick="openMode();" />
</body>
</html>
2.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>2页面</title>
<script language="javascript">
//传此值回父窗体 Js 函数
function reText(){
window.opener.document.getElementById('reText').value=document.getElementById('text').value;
}
</script>
</head>
<body>
<input type="text" id="text" /><input type="button" value="传此值回父窗体" onclick="reText();" />
</body>
</html>
3.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>3页面</title>
<script language="javascript">
//传此值回父窗体 Js 函数
function reText(){
window.opener.document.getElementById('reText').value=document.getElementById('text').value;
}
//弹出模式对话框 Js 函数
function openMode()
{
window.showModalDialog('3.html');
}
</script>
</head>
<body>
<input type="text" id="text" /><input type="button" value="传此值回父窗体" onclick="reText();" />
</body>
</html>
- 上一篇:简体繁体切换问题
- 下一篇:如何保证用户隐私不被网站窃取