Created
September 11, 2019 13:40
-
-
Save Farmatique/138974675fcb24c6ea7ecb86bfd87fbc to your computer and use it in GitHub Desktop.
Iframe send data to parent using postMessage
This file contains hidden or 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
// 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