Skip to content

Instantly share code, notes, and snippets.

@gwa
Last active December 27, 2015 13:19
Show Gist options
  • Save gwa/7332694 to your computer and use it in GitHub Desktop.
Save gwa/7332694 to your computer and use it in GitHub Desktop.
iframe height change listener
// in parent document
// no jQuery required
(function () {
var
ifr = document.getElementById('iframeid'),
vpadding = 50;
window.addEventListener(
"message",
function (ev) {
var d=ev.data.split(':'), k=d[0], v=d[1];
console.log(d);
switch (k) {
case 'h' :
ifr.setAttribute('height', ((v-0)+vpadding)+'px');
break;
}
},
ifr
);
})();
// call in child when height changes
function sendHeight ()
{
if (!parent) {
return;
}
parent.postMessage('h:'+$('body').height(), '*');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment