父子窗口间传值

  1. 父窗口:
  2. url = url + '?winFlg=aaa&winPig=bbb';
  3. window.open(url, '', '');
  4. 子窗口:
  5. var URLParams = new Object();
  6. var aParams = document.location.search.substr(1).split('&');
  7. for (var i=0; i < aParams.length; i++) {
  8. var aParam = aParams[i].split('=');
  9. URLParams[aParam[0]] = aParam[1];
  10. }
  11. alert(URLParams["winFlg"]);
  12. alert(URLParams["winPig"]);
  13. ==================================================================================
  14. 打开一个新窗口,该子窗口调用父对象的方法或变量,这个问题一直没有搞清楚。网上找了些资料,总结一下:
  15. 打开新窗口一般有几种方法,window.open(...),window.showModalDialog(...),以及iframe中嵌套页面这里也一起研究吧,另外还有window.navigate(...)、window.location.href="..."、window.history.back(-1);都是实现同意页面内容跳转的,这里不讨论。
  16. 1、open子窗口:用window.opener代表父窗口的window对象
  17. 2、模态子窗口:间接通过传window对象到子窗口,然后子窗口可获得父窗口的window对象
  18. 3、iframe中子页面:用window.parent代表父窗口的window对象
  19. 父页面:1.htm 代码:
  20. <html>
  21. <head>
  22. <title>打开父子窗口传值研究-父窗口</title>
  23. <script >
  24. var parValue="现在显示了父窗口中的变量值";
  25. function test(){
  26. alert("现在显示了父窗口中的方法正常执行");
  27. }
  28. </script>
  29. </head>
  30. <body >
  31. <input type="button" id="mybutton1" value="打开open新窗口" οnclick="window.open('2.htm');">
  32. <input type="button" id="mybutton2" value="打开modal窗口" οnclick="window.showModalDialog('3.htm',window);" >
  33. <br>
  34. <iframe id="subiframe" name="subiframe" src="4.htm" scrolling="auto" frameborder="1" ></iframe>
  35. </body>
  36. </html>
  37. 2.htm 代码:
  38. <html>
  39. <head>
  40. <title>打开父子窗口传值研究-open打开子窗口</title>
  41. <script>
  42. var buttonValue=window.opener.document.getElementById("mybutton2").value //获取父窗口中的对象
  43. var parentValue=window.opener.parValue; //获取父窗口中的变量
  44. function doParTest(){
  45. window.opener.test(); //调用父窗口中的方法
  46. }
  47. </script>
  48. </head>
  49. <body>
  50. <input type="button" value="open打开子窗口按钮" οnclick="alert('buttonValue:'+buttonValue);alert('parentValue:'+parentValue);doParTest()">
  51. </body>
  52. </html>
  53. 3.htm 代码:
  54. <html>
  55. <head>
  56. <title>打开父子窗口传值研究-打开modal子窗口</title>
  57. <script>
  58. var parentWin=window.dialogArguments;
  59. var buttonValue=parentWin.document.getElementById("mybutton2").value //获取父窗口中的对象
  60. var parentValue=parentWin.parValue; //获取父窗口中的变量
  61. function doParTest(){
  62. parentWin.test(); //调用父窗口中的方法
  63. }
  64. </script>
  65. </head>
  66. <body bgcolor="#FFFFFF" text="#000000">
  67. <input type="button" value="modal子窗口按钮" οnclick="alert('buttonValue:'+buttonValue);alert
  68. ('parentValue:'+parentValue);parentWin.test();">
  69. </body>
  70. </html>
  71. 4.htm 代码:
  72. <html>
  73. <head>
  74. <title>打开父子窗口传值研究-iframe中子窗口</title>
  75. <script>
  76. var buttonValue=window.parent.document.getElementById("mybutton2").value //获取父窗口中的对象
  77. var parentValue=window.parent.parValue; //获取父窗口中的变量
  78. function doParTest(){
  79. window.parent.test(); //调用父窗口中的方法
  80. }
  81. </script>
  82. </head>
  83. <body>
  84. <input type="button" value="iframe中子窗口按钮" οnclick="alert('buttonValue:'+buttonValue);alert('parentValue:'+parentValue);doParTest()">
  85. </body>
  86. </html>
父窗口:

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>
</html>
 



下面是自己写了一个测试例子:
test1:

Java代码 复制代码 收藏代码
  1. <input type="text" id="txt">
  2. <div style="float:left;" id="div" ></div>
  3. <input type="button" value="open" onClick="openWindow()">
  4. <script>
  5. function openWindow(){
  6. var result=window.showModalDialog("test2.html",window);
  7. alert(result);
  8. var arr = result.split('_');
  9. var arrCodes = arr[0].split(',');
  10. var arrNames = arr[1].split(',');
  11. document.getElementById('txt').value=arr[1];
  12. var innerH = '';
  13. for(var i=0;i<arrCodes.length-1;i++){
  14. innerH+='<input type=\"text\" name=\"old\" value=\"'+arrCodes[i]+'\" />'
  15. }
  16. document.getElementById('div').innerHTML=innerH;
  17. }
  18. </script>
<input type="text" id="txt">
<div style="float:left;" id="div" ></div>
<input type="button" value="open" onClick="openWindow()">

<script>
function openWindow(){

  var result=window.showModalDialog("test2.html",window);
	alert(result);
  var arr = result.split('_');
  var arrCodes = arr[0].split(',');
  var arrNames = arr[1].split(',');
  document.getElementById('txt').value=arr[1];
  
  var innerH = '';
  for(var i=0;i<arrCodes.length-1;i++){
  	innerH+='<input type=\"text\" name=\"old\" value=\"'+arrCodes[i]+'\" />'
  }
  document.getElementById('div').innerHTML=innerH;
  
}
</script>



test2:

Java代码 复制代码 收藏代码
  1. <html>
  2. <head>
  3. <script>
  4. function onload(){
  5. var parentWin = window.dialogArguments;
  6. var obj = parentWin.document.getElementsByName('old');
  7. var oldValues = new Array();
  8. for(var i=0;i<obj.length;i++){
  9. oldValues[i]=obj[i].value;
  10. }
  11. var obj = document.getElementsByName('selectValues');
  12. for(var i = 0;i<obj.length;i++){
  13. for(var j=0;j<oldValues.length;j++){
  14. if(obj[i].value == oldValues[j])
  15. { obj[i].checked='checked';}
  16. }
  17. }
  18. }
  19. function ok(){
  20. var industryCodes = new Array();
  21. var industryNames = new Array();
  22. var tmpCodes = '';
  23. var tmpNames = '';
  24. var codes = document.getElementsByName("selectValues");
  25. var names = document.getElementsByName("selectNames");
  26. for(var i =0 ,j=0 ; i <codes.length;i++){
  27. if(codes[i].checked){
  28. tmpCodes += codes[i].value+',';
  29. tmpNames += names[i].value+',';
  30. j++;
  31. }
  32. }
  33. var tmpValue =tmpCodes+'_'+tmpNames;
  34. window.returnValue = tmpValue;
  35. window.close();
  36. }
  37. </script>
  38. </head>
  39. <body οnlοad="onload()">
  40. <input type="text" id="test1" value="" />
  41. <input type="checkbox" name="selectValues" value="1" />文本1<input type="hidden" name="selectNames" value="文本1" />
  42. <br>
  43. <input type="checkbox" name="selectValues" value="2" />文本2<input type="hidden" name="selectNames" value="文本2" />
  44. <br>
  45. <input type="checkbox" name="selectValues" value="3" />文本3<input type="hidden" name="selectNames" value="文本3" />
  46. <br>
  47. <input type="button" οnclick="ok()" />
  48. </body>
  49. </html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值