javascript中子視窗與父視窗互動操作

來源:互聯網
上載者:User
此文轉載於:http://blog.csdn.net/fenglibing/archive/2007/09/11/1781243.aspx

1、從子視窗中往父視窗傳值如父頁面有一個控制的id是choosedProvider,那麼這樣可以把值傳給父頁面:opener.document.getElementById("choosedProvider”]).value=”Tom”;2、從父視窗傳值給子視窗,如子視窗中有id為T的控制項,此時可以用如下傳值:<script>var newWindow;function openWindow(url){newWindow = window.open(“new.htm”, "", "width=400,height=300 ");}function changeChild(){newWindow.document.getElementById("T").value="我變了";}</script>       先抓住其控制代碼,然後通過控制代碼操作,其實比較上面的子視窗往父視窗中傳值,就是操作的對象改變了一下。3、子視窗關閉並重新整理父口,這裡有兩種方式:a)         子視窗關閉時關閉父視窗:opener.location.reload();window.close();b)        父視窗去檢測,子視窗是否已經關閉了,在設定的一段時間之後自動重新整理:<script>var newWindow;var timer;function openWindow(url) {       newWindow = window.open(url, "", "width=400,height=300,resizable=yes");       timer = setInterval("updateAfterClose()", 1000);} function updateAfterClose() {       //父視窗去檢測子視窗是否關閉,然後通過自我重新整理,而不是子視窗去重新整理父視窗       if(newWindow.closed == true) {       clearInterval(timer);       self.location.reload(); // 主視窗重新整理       return;       }}</script>4、父視窗關閉的時候關閉子視窗。這個就是用方法去檢測父視窗的onunload事件:<script>var newWindow;function openWindow(url) {newWindow = window.open(url, "", "width=400,height=300,resizable=yes"); }function closeChild(){newWindow.close();}</script>在body加上onunload事件:<body onunload="closeChild()">5、子視窗中關閉父視窗,這個功能可能相對要少用一點:<script>function closeParent(){opener.close();}</script><input type=button value="關閉父視窗" onclick=closeParent()>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.