Skip to content

Instantly share code, notes, and snippets.

@Farmatique
Created September 11, 2019 13:40
Show Gist options
  • Save Farmatique/138974675fcb24c6ea7ecb86bfd87fbc to your computer and use it in GitHub Desktop.
Save Farmatique/138974675fcb24c6ea7ecb86bfd87fbc to your computer and use it in GitHub Desktop.
Iframe send data to parent using postMessage
// source: https://stackoverflow.com/questions/9162933/make-iframe-height-dynamic-based-on-content-inside-jquery-javascript
//parent:
window.addEventListener('message', function(e) {
var $iframe = jQuery("#myIframe");
var eventName = e.data[0];
var data = e.data[1];
switch(eventName) {
case 'setHeight':
$iframe.height(data);
break;
}
}, false);
//iframe:
function resize() {
var height = document.getElementsByTagName("html")[0].scrollHeight;
window.parent.postMessage(["setHeight", height], "*");
}
//iframe body:
<body onLoad="resize();">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment