Skip to content

Instantly share code, notes, and snippets.

@LukeFF
Last active September 7, 2016 10:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LukeFF/95e4fd8e2696325f4f413a7f52570e36 to your computer and use it in GitHub Desktop.
Save LukeFF/95e4fd8e2696325f4f413a7f52570e36 to your computer and use it in GitHub Desktop.
automatic Iframe resizing without Cross-Origin issues
<body onLoad="resizeParent();">
<script type="text/javascript">
function resizeParent() {
var height = document.getElementsByTagName("html")[0].scrollHeight;
window.parent.postMessage(["setIframeHeight", height], "*");
}
</script>
</body>
<iframe id="superFrame" src="iframe.html" style="width: 100%; border: none;" scrolling="no" height="500"></iframe>
<script type="text/javascript">
window.addEventListener('message', function(e) {
var iframe = document.getElementById('superFrame');
var eventName = e.data[0];
var height = e.data[1];
switch(eventName) {
case 'setIframeHeight':
iframe.height = height;
break;
}
}, false);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment