Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hi,
Question: Are there any restrictions in the Shopify Theme Engine that blocks "message" events from an iframe? I can't seem to listen for "message" events from an iframe that links to an external web page in an App Embed Block however I can do the same if I simply host the html/css/js code on locally.
Context:
I'm trying to listen for messages from an iframe in my App Embed Block. The iframe links to a webpage that sends a message with the data, "close-iframe", to window.top on clicking a button on the page.
The code for listening for the message in the app embed block is:
window.addEventListener('message', ({ data }) => {
console.log("message received from iframe: ", data);
if (data === "close-iframe") {
console.log("close message received from iframe: ", data);
//if we get a message from the iframe asking us to close the iframe, make the iframe invisible
setIframeVisible = false;
document.getElementById("iframe").classList.add("invisible");
}
});
The code in the linked webpage in the iframe that sends the message is:
window.top.postMessage("close-iframe")
This setup works flawlessly on localhost but doesn't in the App Embed Block. The interesting thing is that my event listener does capture other messages like "RemoteUI::Initialize".
Solved! Go to the solution
This is an accepted solution.
Solved -- for some reason it just didn't work in the theme editor. It works as expected on the actual test store's link.
This is an accepted solution.
Solved -- for some reason it just didn't work in the theme editor. It works as expected on the actual test store's link.
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024