Question:
I am facing issues with dynamic iframe height on my website, specifically when links inside the iframe are clicked. The height of the section containing the iframe does not adjust to the new content.
Product:
Wix Editor
What are you trying to achieve:
I want the section that contains the iframe to dynamically adjust its height based on the content loaded within the iframe. Additionally, I would like to track link clicks within the iframe so that when a user tries to open a link in a new tab, it loads both the link’s content and the main page that hosts the iframe, making the iframe appear seamless.
What have you already tried:
Adding JavaScript to the iframe page:
javascript
window.addEventListener(‘load’, function() {
let observer = new MutationObserver(() => {
let height = document.body.scrollHeight;
window.parent.postMessage({ height: height }, “*”);
});
observer.observe(document.body, { childList: true, subtree: true });
// Detect changes in URL (when navigating within the iframe)
window.addEventListener('popstate', function() {
let height = document.body.scrollHeight;
window.parent.postMessage({ height: height }, "*");
});
});
Modifying code on the main page:
javascript
$w.onReady(function () {
let iframe = $w(“#myIframe”);
iframe.onMessage((event) => {
if (event.data.height) {
iframe.height = event.data.height + “px”;
}
});
});
Edit Custom Code (dashboard settings):
html
Additional information:
My website is iordanisjewelry.gr, and the iframe is from iordanis-frame-categoryembed.jewelershowcase.com, an external source. I am looking for any advice or solutions that the community may have on this topic, as I believe it may help not only me but others facing similar issues.
If anyone has a working solution and is available for freelance assistance, please let me know—I’d be happy to discuss compensation.