Created
April 12, 2017 21:04
-
-
Save topicus/c8ff538d32271077772f9ddee7d72f91 to your computer and use it in GitHub Desktop.
How to automatically resize an iframe based on their content
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
<script type="text/javascript"> | |
var node = document.scripts[document.scripts.length - 1]; | |
var parent = node.parentElement; | |
var iframe = document.createElement('iframe'); | |
iframe.setAttribute('src', 'http://example.com'); | |
iframe.width = '100%'; | |
iframe.frameBorder = 0; | |
parent.insertBefore(iframe, node.nextSibling); | |
window.addEventListener('message', function(e){ iframe.height = e.data }, false); | |
</script> |
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
// Add to the page that needs to be embedded. | |
if(parent.postMessage) { | |
var refresh = setInterval(() => parent.postMessage(document.body.scrollHeight, '*'), 500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment