Skip to content

Instantly share code, notes, and snippets.

@Phil-B
Created June 7, 2011 14:30
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 Phil-B/1012359 to your computer and use it in GitHub Desktop.
Save Phil-B/1012359 to your computer and use it in GitHub Desktop.
Auto-resize a same-domain iframe to the height of it's content
(function($) {
$.fn.extend({
iFrameAutoHeight: function() {
return this.each(function() {
var obj = $(this);
obj.height(0); //avoid flash at full height
if (obj.contents().find('body').height() > 0) { // already loaded - from cache (ie only)
resize(obj);
} else {
obj.load(function() {
resize(obj);
})
}
});
function resize(obj) {
obj.height(obj.contents().find('body').height());
}
}
});
})(jQuery);
@Phil-B
Copy link
Author

Phil-B commented Jun 7, 2011

This presumes that the iframe content is static, so the resize event isn't used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment