Skip to content

Mechanism for sending messages between current tab, iframe, background? #338

Answered by aklinker1
ongnxco asked this question in Q&A
Discussion options

You must be logged in to vote

Ahh, defineWindowMessaging was more created for a website to send a message to a extension, rather than communicating with other frames on the same page... So that library will need some changes to get it working for your use-case.

To get this working without a library:

// inside the top frame
window.addEventListener("message", (event) => {
  console.log("request:", event.data);
  event.source.postMessage("response-data");
});
// inside your iframe
window.top.postMessage("request-data");
window.addEventListener("message", (event) => {
  // Do something with the response
  console.log("response:", event.data);
}, { once: true });

The reason the library doesn't work, is it sends messages to w…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by ongnxco
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants