javascript使用window.opener在两个页面之间传递值

[size=medium][align=center][b]javascript使用window.opener在两个页面之间传递值[/b][/align][/size]
1、使用第一个页面打开第二个页面,通过第二个页面传递值给第一个页面
<!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=utf-8">
<title>Untitled Document</title>
</head>
<body>
<form name="form1" action="test.htm" method="post">
客户ID:<input type="text" name="cid" value="" id="cid"><br/>
客户名称:<input type="text" name="cname" value="" id="cname"><br/>
<input type="button" name="ok" value="请选择客户" onclick="openWin();">
</form>
</body>

<script type="text/javascript">
/*
* window.open(sURL,sName,sFeatrues);
* sURL:要打开文件的URL地址
* sName:指定打开窗口的名字:_self当前窗口 _blank:在新窗口打开
* sFeatures:装饰
*/
function openWin() {
window.open("msg.htm","_blank","height=200,width=200,status=yes,toolbar=no,menubar=no");
}

function setValue(cid,cname) {
document.getElementById("cid").value = cid;
document.getElementById("cname").value = cname;
}
</script>
</html>

2、第二个页面获取第一个页面的对象,并调用第一个页面的方法,将第一个页面所需要的值传递给第一个页面
<!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=utf-8">
<title>Untitled Document</title>
<script type="text/javascript">
function viewData(cid ,cname) {
//获取通过showModalDialog传过来的window对象
var sdata = window.opener;

//设置第一个页面的属性值
// sdata.document.getElementById("cid").value = cid;
// sdata.document.getElementById("cname").value = cname;

sdata.setValue(cid,cname);
//关闭Dialog窗口
window.close();
}
</script>
</head>
<body>
<table border="1">
<tr>
<td>操作</td>
<td>客户ID</td>
<td>客户名称</td>
</tr>
<tr>
<td><button onclick="viewData('001','深圳华为')">选择</button></td>
<td>001</td>
<td>深圳华为</td>
</tr>
<tr>
<td><button onclick="viewData('002','用友软件')">选择</button></td>
<td>002</td>
<td>用友软件</td>
</tr>
</table>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值