下對我的作品在Chrome,火狐,IE瀏覽器(沒有測試更多的瀏覽器)
假設3個文件
- ( www.mydomain.com/parent.html),其中包含了「main'文檔與鏈接
- (bills.mydomain.com/child.html),將通過鏈接打開的頁面的頁面
- (www.mydomain.com/dispatcher.html)以後
首先闡述在parent.html創建所有3個文件的域屬性設置爲mydomain.com
<script>
document.domain="mydomain.com";
</script>
隱藏的iframe,其名稱屬性爲例如「hiddenframe」。同時創建一些可能稍後會收到響應的函數。
parent.html現在應該是這樣的:
<script>
document.domain="mydomain.com";
function fx(msg)//receives the response
{
alert(msg)
}
</script>
<iframe name="hiddenframe" style="display:none"></iframe>
<a href="http://bills.mydomain.com/child.html" target="_blank">click</a>
在child.html你現在就可以將文檔加載到隱藏的iframe內parent.html
<script>
document.domain="mydomain.com";
window.open('http://www.mydomain.com/dispatcher.html','hiddenframe');
</script>
(不要混淆在面對使用window.open()
這裏,不會打開一個新窗口,頁面會被加載到parent.html中的iframe中)
在dispatcher.html中,您現在可以調用父代中的函數。HTML
<script>
document.domain="mydomain.com";
parent.fx('you just got some response');
</script>
當你只需要重新加載它是一個有點容易的parent.html。
再次設置在parent.html和child.html的document.domain的屬性(你不需要在parent.html iframe和dispatcher.html)
在parent.html還設置了窗口的名稱 - 屬性,例如
<script>
window.name="parentTab";
</script>
在child.html你現在可以訪問parentTab
-window(標籤)
<script>
document.domain="mydomain.com";
window.open('http://www.mydomain.com/parent.html','parentTab');
</script>
......或者乾脆用「parentTarget」作爲孩子一個鏈接或表單的目標特性。 HTML
我相信'window.open'應該返回一個新的_Window_對象,並且如果你沒有遇到同樣的起源策略的問題,你可能能夠將一個監聽器從父對象附加到這個對象上,或者設置一個間隔檢查一些變量。 –
@PaulS。,它window.open不會在新標籤中打開。 – Alon
@Alon它在Firefox中執行。 –