Created
September 4, 2019 11:07
-
-
Save droid001/9dd627fddb4ae20696bb1a6779307e16 to your computer and use it in GitHub Desktop.
resize iframe 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
// child.html | |
let height; | |
const sendPostMessage = () => { | |
if (height !== document.getElementById('container').offsetHeight) { | |
height = document.getElementById('container').offsetHeight; | |
window.parent.postMessage({ | |
frameHeight: height | |
}, '*'); | |
console.log(height); | |
} | |
} | |
window.onload = () => sendPostMessage(); | |
window.onresize = () => sendPostMessage(); | |
document.getElementById('add-para').onclick = () => sendPostMessage(); | |
document.getElementById('remove-para').onclick = () => sendPostMessage(); | |
// parent.html | |
<iframe id="i" src="child.html" style="width:100%;border:none;"></iframe><script>window.onmessage=e=>{e.data.hasOwnProperty("frameHeight")&&(document.getElementById("i").style.height=`${e.data.frameHeight+30}px`)}</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment