五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > javascript調(diào)用父窗口(父頁面)的方法[摘自網(wǎng)上]

javascript調(diào)用父窗口(父頁面)的方法[摘自網(wǎng)上]

文章來源:365jz.com     點擊數(shù):1236    更新時間:2009-10-05 21:44   參與評論
window.parent與window.opener的區(qū)別 javascript調(diào)用主窗口方法
1:   window.parent 是iframe頁面調(diào)用父頁面對象
舉例:
a.html
Html代碼 復制代碼
  1. <html>  
  2.     <head><title>父頁面</title></head>  
  3. <body>  
  4.     <form name="form1" id="form1">  
  5.          <input type="text" name="username" id="username"/>  
  6.     </form>  
  7.     <iframe src="b.html" width=100%></iframe>  
  8. </body>  
  9. </html>  
 
如果我們需要在b.htm中要對a.htm中的username文本框賦值,就如很多上傳功能,上傳功能頁在Ifrmae中,上傳成功后把上傳后的路徑放入父頁面的文本框中
我們應該在b.html中寫
Html代碼 復制代碼
  1. <script type="text/javascript">  
  2.     var _parentWin = window.parent ;   
  3.     _parentWin.form1.username.value = "xxxx" ;   
  4. </script>  

實例地址:  http://www.cnspry.cn/blog/attachments/window.parent 實例/a.html
源碼:
1.a.html
Html代碼 復制代碼
  1. <html>  
  2. <head>  
  3.     <title>主頁面</title>  
  4.     <script>  
  5.         /** 為測試IFrame子窗口調(diào)用父窗口的全局變量而添加的測試變量 */   
  6.         var parentVairous = "為測試IFrame子窗口調(diào)用父窗口的全局變量而添加的測試變量";   
  7.         function parentInvokeIFrame()   
  8.         {   
  9.                 var iframeTest = document.frames["iframeTest"]; //使用document.getElementById("iframeTest");同樣可以   
  10.                 alert(iframeTest.document.body.innerHTML);   
  11.                 alert(iframeTest.iFrameVair);   
  12.         }   
  13.     </script>  
  14. </head>  
  15. <body>  
  16. <form name="form1" id="form1">  
  17.     <input type="text" name="username" id="username"/>  
  18.     <input type = "button" value = "父窗口調(diào)用IFrame子窗口中的內(nèi)容" onclick = 'parentInvokeIFrame()'/>  
  19. </form>  
  20. <iframe src="b.html" width = '100%' id = 'iframeTest'></iframe>  
  21. </body>  
  22. </html>  

1.b.html
Html代碼 復制代碼
  1. <html>  
  2.      <head>  
  3.          <title></title>  
  4.          <script type="text/javascript">  
  5.             /** 為測試父窗體調(diào)用IFrame子窗體的全局函數(shù)而添加的子窗口全局函數(shù) */   
  6.          var iFrameVair = "測試父窗體調(diào)用IFrame子窗體的全局函數(shù)";   
  7.             
  8.          function UpdateParent()   
  9.          {   
  10.              var _parentWin = window.parent ;   
  11.              _parentWin.form1.username.value = "xxxx" ;   
  12.          }   
  13.             
  14.          function childInvokeParent()   
  15.          {   
  16.                 var parentVairous = window.parent.window.parentVairous;   
  17.                 alert(parentVairous);      
  18.          }   
  19.        </script>  
  20.     </head>  
  21. <body>  
  22.      <form name="form1" id="form1">  
  23.          <p>  </p>  
  24.          <p align="center">  
  25.             <input type = "button"    
  26.                    name = "button"    
  27.                    id = "button"    
  28.                    value = "更新主頁面的UserName內(nèi)容"    
  29.                    onclick = "UpdateParent()">  
  30.             <input type = "button"  
  31.                          name = "button2"  
  32.                          id = "button2"  
  33.                          value = "測試IFrame子窗口調(diào)用父窗口的全局變量"  
  34.                          onclick = "childInvokeParent();"/>  
  35.          </p>  
  36.          <p>  </p>  
  37.      </form>  
  38. </body>  
  39. </html>  
  ps:不能跨域獲取,例如iframe的src是'http://www.xxx.ccc/'就不可以
2:   window.opener 是window.open 打開的子頁面調(diào)用父頁面對象
實例地址:  http://www.cnspry.cn/blog/attachments/window.opener 實例/a.html
源碼:
2.a.html
Html代碼 復制代碼
  1. <html>  
  2. <head>  
  3.      <title>主頁面</title>  
  4.      <script type="text/javascript">  
  5.      /** 為測試IFrame子窗口調(diào)用父窗口的全局變量而添加的測試變量 */     
  6.      var parentVairous = "為測試IFrame子窗口調(diào)用父窗口的全局變量而添加的測試變量";    
  7.         
  8.      /**    
  9.       *  因為不同于IFrame(IFrame有id,window.open()與IFrame的父子窗口的模式不同),   
  10.       *  所以當是通過window.open()方法打開一個新窗口使, 必須有一個新窗口的對象    
  11.       *  當然必須先讓子窗口彈出來, 才能調(diào)用子窗口中的變量, 否則拋出異常   
  12.       */   
  13.      var OpenWindow;   
  14.         
  15.      function openSubWin()   
  16.      {   
  17.          OpenWindow = window.open('b.html', 'newwindow', 'height=1024width=1300top=0left=0toolbar=nomenubar=yesscrollbars=yes,resizable=yes,location=nostatus=no');   
  18.      }   
  19.      function parentInvokeChild()     
  20.      {     
  21.          if(OpenWindow)//當然必須先讓子窗口彈出來, 才能調(diào)用子窗口中的變量, 否則拋出異常            
  22.          {   
  23.                alert(OpenWindow.iFrameVair);   
  24.          }   
  25.      }    
  26.      </script>  
  27. </head>  
  28. <body>  
  29.     <form name="form1" id="form1">  
  30.         <input type="text" name="username" id="username"/>  
  31.         <input type="button" value="彈出子頁面" onclick = "openSubWin()">  
  32.         <input type="button" value="測試調(diào)用彈出窗口中的全局變量" onclick = "parentInvokeChild()">  
  33.     </form>  
  34. </body>  
  35. </html>  

2.b.html  
Html代碼 復制代碼
  1. <html>  
  2.     <head>  
  3.         <title>子頁面</title>  
  4.         <script type="text/javascript">  
  5.          /** 為測試父窗體調(diào)用IFrame子窗體的全局函數(shù)而添加的子窗口全局函數(shù) */     
  6.          var iFrameVair = "測試父窗體調(diào)用IFrame子窗體的全局函數(shù)";   
  7.          function UpdateParent()   
  8.          {   
  9.               var _parentWin = window.opener;   
  10.               _parentWin.form1.username.value = "xxxx" ;   
  11.          }   
  12.          function childInvokeParent()     
  13.          {     
  14.               var parentVairous = window.opener.window.parentVairous;     
  15.               alert(parentVairous);        
  16.          }   
  17.         </script>  
  18.     </head>  
  19. <body>  
  20. <form name="form1" id="form1">  
  21. <p>  </p>  
  22. <p align="center">  
  23.     <input type="button"    
  24.                onclick = "UpdateParent();"    
  25.                name="button"    
  26.                id="button"    
  27.                value="更新主頁面的UserName內(nèi)容">  
  28.     <input type = "button"     
  29.            name = "button2"     
  30.            id = "button2"     
  31.            value = "測試IFrame子窗口調(diào)用父窗口的全局變量"     
  32.            onclick = "childInvokeParent();"/>     
  33. </p>  
  34. <p>  </p>  
  35. </form>  
  36. </body>  
經(jīng)過hanjs的提醒,確實需要注意的是,模態(tài)窗口的子窗口是沒有辦法修改父窗口頁面中的任何內(nèi)容的。
例如修改:OpenWindow = window.open('b.html', 'newwindow', 'height=1024, width=1300, top=0, left=0, toolbar=no, menubar=yes, scrollbars=yes,resizable=yes,location=no, status=no');
為:OpenWindow = window.showModalDialog("b.html",'newwindow',"dialogHeight:100px,center:yes,resizable:no,status:no");
在子窗口中當希望修改父窗口中的內(nèi)容時,會彈出“某某”為空或不是對象的錯誤,而這里的“某某”就是你想修改的父窗口中的內(nèi)容

作者:LiFuyun
出處:http://lifuyun.cnblogs.com
本文版權歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。

如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會為你解答??! 點擊進入論壇

發(fā)表評論 (1236人查看0條評論)
請自覺遵守互聯(lián)網(wǎng)相關的政策法規(guī),嚴禁發(fā)布色情、暴力、反動的言論。
昵稱:
最新評論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學習

業(yè)務咨詢

· 技術支持
· 服務時間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權所有 鄂ICP備17013400號