Skip to content

Instantly share code, notes, and snippets.

@adressler
Created April 15, 2011 10:20
Show Gist options
  • Select an option

  • Save adressler/921491 to your computer and use it in GitHub Desktop.

Select an option

Save adressler/921491 to your computer and use it in GitHub Desktop.
Resize iFrames across domains
<!-- taken from http://blog.johnmckerrell.com/2006/10/22/resizing-iframes-across-domains/ -->
<!--
My Domain
the page that includes the iFrame
-->
<script>
function updateIFrame(height) {
var iframe = document.getElementById('myiframe');
iframe.setAttribute('height', height);
}
</script>
<!--
My Domain
iframe-resize.html - the resize helper file
-->
<script>
function onLoad() {
var params = window.location.search.substring(1).split('&');
var height;
for (var i = 0, l = params.length; i < l; ++i) {
var parts = params[i].split('=');
switch(parts[0]) {
case 'height':
height = parseInt( parts[1] );
break;
}
}
if (typeof(height) == 'number') {
window.top.updateIFrame(height);
}
}
window.onload = onLoad;
</script>
<!--
Foreign Domain
the page, that gets loaded into an iframe
-->
<iframe id="resizeHelperIframe" width="1" height="1"></iframe>
<script>
var iframe = document.getElementById('resizeHelperIframe');
var height = Math.max(document.body.offsetHeight, document.body.scrollHeight);
iframe.src = 'http://MyDomain.com/iframe-resize.html?height=' + height;
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment