JavaScript window.opener的用法

本文介绍如何利用window.opener在子窗口中操作打开它的父窗口,包括刷新父窗口、给父窗口元素赋值及关闭父窗口等方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

window.opener主要用于通过子页面操纵打开子页面的父页面。通过这种方法,子页面可以象操纵本页面一样操纵父页面。

通过Windows.opener可以获取父页面对象,接下来就可以象操纵本页面一样操纵父页面。这里重点列出几个特殊的方法:

window.opener.location.reload(): 该方法可以在子页面中刷新父页面。

window.opener.close():该方法在子页面中关闭父页面。

window.opener.document.operator:该方法就是子页面操纵父页面的方法,其中operator就是和本页面一样的具 体操纵。Eg:

 window.opener.document.getElementById('testValue').value='111111' //在子页面中给id为testValue的元素赋值。

具体举例如下:

父页面opner.html

  1. <html>  
  2. <head>  
  3. <meta http-equiv="Content-Type"  content= "text/html; charset=gb2312" >  
  4. <title>opner</title>  
  5. <mce:script type="text/javascript" ><!--  
  6. function  tetsVlaue(){  
  7.         document.getElementById('childTest' ).document.getElement( 'refreshParent' ).value= "111111" ;  
  8. }  
  9. // --></mce:script>   
  10. </head>  
  11. <body>  
  12. <input type="button"  value= "open"  onclick= "window.open('child.html','childTest')"  />  
  13. <br>  
  14. <input type="text"  id= "testValue"  name= "testValue"  value= "123"  readonly />  
  15. <br>  
  16. <input type="button"  value= "testChild"  onclick= "testChild()" >  
  17. </body>  
  18. </html>  
  19. 子页面child.html  
  20. <html>  
  21. <head>  
  22. <meta http-equiv="Content-Type"  content= "text/html; charset=gb2312" >  
  23. <title>childTest</title>  
  24. </head>  
  25. <body>  
  26. <mce:script type="text/javascript" ><!--  
  27. function  refreshParent(){  
  28.   window.opener.location.reload();  
  29.   window.opener.document.getElementById('testValue' ).value= '111111' ;  
  30. }  
  31.    
  32. function  closeparent(){  
  33. window.opener.close();  
  34. }  
  35. // --></mce:script>   
  36. <input type="button"  id= "refreshParent"  value= "refreshParent"  onclick= "refreshParent()" >  
  37. <br>  
  38. <input type="button"  value= "closeparent"  onclick= "closeparent()" >  
  39.    
  40. </body>
  41. </html>  

 

该例实现在子页面child.html中关闭,从新加载父页面opener.html,同时为新页面相应元素赋值,注意父页面中文本框的变化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值