public
Last active

Auto-resize a same-domain iframe to the height of it's content

  • Download Gist
jquery.iFrameAutoHeight.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
(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);

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.