I have a cross-domain iframe inside one of my page, its content should use the window.parent.postMessage
method to notify it is rendered to the containing page (so it can adjust iframe's height to its content). It works quite well, but only once, if I start clicking in the iframe and browsing different pages, the load event does not get triggered anymore even if all the pages in the iframe contains the same JavaScript code (see below).
here's the container's code let's say container.js:
function onMessage(jqEvent) {
if (jqEvent.originalEvent.data.iframe) {
var iframeContentHeight = jqEvent.originalEvent.data.iframe;
$('iframe').height(iframeContentHeight + 300);
}
}
$(window).on('message.socialPanel', onMessage);
and here is the code inside the iframe (it is in an external file, let's call it iframe.js):
$(window).on('load', function () {
var height = $('html').height();
window.parent.postMessage({'iframe': height}, '*');
});
I have a cross-domain iframe inside one of my page, its content should use the window.parent.postMessage
method to notify it is rendered to the containing page (so it can adjust iframe's height to its content). It works quite well, but only once, if I start clicking in the iframe and browsing different pages, the load event does not get triggered anymore even if all the pages in the iframe contains the same JavaScript code (see below).
here's the container's code let's say container.js:
function onMessage(jqEvent) {
if (jqEvent.originalEvent.data.iframe) {
var iframeContentHeight = jqEvent.originalEvent.data.iframe;
$('iframe').height(iframeContentHeight + 300);
}
}
$(window).on('message.socialPanel', onMessage);
and here is the code inside the iframe (it is in an external file, let's call it iframe.js):
$(window).on('load', function () {
var height = $('html').height();
window.parent.postMessage({'iframe': height}, '*');
});
another suggestion - onload
is being triggered just once, yes, even when you change the src
URL. But if you want it to be triggered again - just detach the iframe from DOM and attach it back again (when changing the URL). It did the trick for me.
I ran into exactly the same problem. @n00dl3's solution might not work in all situations - it didn't for me. It's due to a race condition - the load
handler needs to be attached before the iframe
loads its src
. In some cases, moving the JS inline would move it early enough, in my case it didn't.
Here's how I solved it:
src
and onload
from the iframe
tag:<iframe id="foo"></iframe>
function resize() {
// do the resizing here
}
load
handler:document.getElementById("foo").setAttribute("onload", "resize()");
src
:document.getElementById("foo").setAttribute("src", "https://example./bar");
Boom, fires every time.
Ok, I finally found the solution (an explanation would be wele though).
I had to put the iframe's javascript in an inline <script>
tag and not in an external one.
So my previous code :
<script type="text/javascript" src="/assets/js/build.js" ></script>
has bee:
<script type="text/javascript">
window.onload = function () {
var height = $('html').height();
window.parent.postMessage({'iframe': height}, '*');
}
</script>