簡體   English   中英

如何將事件從父窗口發送到其子窗口?

[英]How can I send an event from parent window to its child window?

我想從父窗口發送一些事件/數據到它的子窗口。 我怎么能這樣做?

我試過postMessage ,但沒有用。

它現在起作用了,早些時候我犯了一些語法錯誤。 這是相同的正確代碼。

//parent Window
childWindow=window.open("http:/localhost/abcd.php","_blank","width=500,height=500");
childWindow.postMessage('message',"http://localhost:80/child.php");


//child Window
var newUrl='';
window.addEventListener(
 "message",
 function(e) { 

console.log(e.data);//your data is captured in e.data 
}, false);

如果您所指的子窗口是 iFrame,那么postMessage就是您堅持使用的方法。

在沒有看到您嘗試過的任何代碼的情況下,很難說您對postMessage的特定用法不起作用 - 但是,這里是MDN 文章中工作postMessage示例的鏈接,以及帶有您可以從中剖析代碼的全功能示例

其中之一應該能夠向您展示為什么您的實現不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM