Skip to content

Instantly share code, notes, and snippets.

@jhit
Forked from LukeFF/iframe.html
Created September 7, 2016 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhit/eab6286c8236425341710fea382bca7d to your computer and use it in GitHub Desktop.
Save jhit/eab6286c8236425341710fea382bca7d 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="superiFrame" src="..." 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