Last active
December 27, 2015 13:19
-
-
Save gwa/7332694 to your computer and use it in GitHub Desktop.
iframe height change listener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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