父窗口向子窗口传值

父窗口向子窗口传值:

url = url + '?winFlg=aaa&winPig=bbb';

window.open(url, '', '');

子窗口:

var URLParams = new Object();
var aParams = document.location.search.substr(1).split('&');
for (var i=0; i < aParams.length; i++) {
var aParam = aParams[i].split('=');
URLParams[aParam[0]] = aParam[1];
}

alert(URLParams["winFlg"]);

alert(URLParams["winPig"]);

==================================================================================

打开一个新窗口,该子窗口调用父对象的方法或变量,这个问题一直没有搞清楚。网上找了些资料,总结一下:

打开新窗口一般有几种方法,window.open(...),window.showModalDialog(...),以及iframe中嵌套页面这里也一起研究吧,另外还有window.navigate(...)、window.location.href="..."、window.history.back(-1);都是实现同意页面内容跳转的,这里不讨论。

1、open子窗口:用window.opener代表父窗口的window对象

2、模态子窗口:间接通过传window对象到子窗口,然后子窗口可获得父窗口的window对象

3、iframe中子页面:用window.parent代表父窗口的window对象


父页面:1.htm 代码:

<html>
<head>
<title>打开父子窗口传值研究-父窗口</title>

<script >
var parValue="现在显示了父窗口中的变量值";

function test(){
alert("现在显示了父窗口中的方法正常执行");
}

</script>
</head>
<body >
<input type="button" id="mybutton1" value="打开open新窗口" οnclick="window.open('2.htm');">
<input type="button" id="mybutton2" value="打开modal窗口" οnclick="window.showModalDialog('3.htm',window);" >
<br>
<iframe id="subiframe" name="subiframe" src="4.htm" scrolling="auto" frameborder="1" ></iframe>
</body>
</html>


2.htm 代码:

<html>
<head>
<title>打开父子窗口传值研究-open打开子窗口</title>
<script>
var buttonValue=window.opener.document.getElementById("mybutton2").value //获取父窗口中的对象
var parentValue=window.opener.parValue; //获取父窗口中的变量

function doParTest(){
window.opener.test(); //调用父窗口中的方法

}
</script>
</head>
<body>

<input type="button" value="open打开子窗口按钮" οnclick="alert('buttonValue:'+buttonValue);alert('parentValue:'+parentValue);doParTest()">

</body>
</html>


3.htm 代码:

<html>
<head>
<title>打开父子窗口传值研究-打开modal子窗口</title>
<script>
var parentWin=window.dialogArguments;
var buttonValue=parentWin.document.getElementById("mybutton2").value //获取父窗口中的对象
var parentValue=parentWin.parValue; //获取父窗口中的变量
function doParTest(){
parentWin.test(); //调用父窗口中的方法
}
</script>
</head>

<body bgcolor="#FFFFFF" text="#000000">

<input type="button" value="modal子窗口按钮" οnclick="alert('buttonValue:'+buttonValue);alert

('parentValue:'+parentValue);parentWin.test();">
</body>
</html>


4.htm 代码:

<html>
<head>
<title>打开父子窗口传值研究-iframe中子窗口</title>
<script>
var buttonValue=window.parent.document.getElementById("mybutton2").value //获取父窗口中的对象
var parentValue=window.parent.parValue; //获取父窗口中的变量

function doParTest(){
window.parent.test(); //调用父窗口中的方法
}
</script>
</head>
<body>

<input type="button" value="iframe中子窗口按钮" οnclick="alert('buttonValue:'+buttonValue);alert('parentValue:'+parentValue);doParTest()">

</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值