Home > JavaScript, jQuery > Resize iFrame to height of content, from iFrame, without an ID

Resize iFrame to height of content, from iFrame, without an ID

December 8th, 2008

There must be a more graceful way to reference the current iframe without knowing its ID?

function resizeIFrame() {
	var iframe = $('iframe',parent.document.body);
	var doc;
	var iHeightPadding;
	for (var i=0, j=iframe.length; i<j; i++) {
		if (iframe[i].contentDocument) {
			doc = iframe[i].contentDocument;
			iHeightPadding = 35;
		} else {
			doc = iframe[i].contentWindow.document;
			iHeightPadding = 0;
		}
		if (doc === document) {
			//located our iframe!
			$(iframe[i]).height(doc.body.scrollHeight + iHeightPadding);
			break;
		}
	};
}
Share this Article:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Add to favorites
  • email
  • FriendFeed
  • LinkedIn
  • Netvibes
  • Reddit
  • Slashdot
  • StumbleUpon
  • Tumblr
  • Twitter

booshtukka JavaScript, jQuery

  1. No comments yet.
  1. No trackbacks yet.