Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Resize an IFrame to the Size of Its Contents Without Displaying Scroll Bars


View products that this article applies to.

Summary

In certain situations, you may want to resize an IFrame so that all of its document contents are visible, but no scroll bars are displayed on the page. This article describes how to do this.

↑ Back to the top


More information

To determine the size of the IFrame's contents, you must access the height and width properties of the underlying IFrame document. Because you only have scripting access to pages that are hosted in the same domain, you can only access the properties to the pages that are hosted within the same domain (for example, Cross Frame Scripting). Therefore, the source document of the IFrame must be from the same domain as the page that contains the IFrame.

The following code demonstrates how to resize an IFrame in this way. Create a new .htm document, and paste the following HTML code. In the SRC attribute for the IFrame, you must supply an HTML page from the same domain that the IFrame loads.

NOTE: This technique may not work correctly if there are absolutely positioned elements that are residing within the IFrame.
<HTML>
<HEAD>
<SCRIPT LANGUAGE=javascript>
<!--
function reSize()
{
	try{	
	var oBody	=	ifrm.document.body;
	var oFrame	=	document.all("ifrm");
		
	oFrame.style.height = oBody.scrollHeight + (oBody.offsetHeight - oBody.clientHeight);
	oFrame.style.width = oBody.scrollWidth + (oBody.offsetWidth - oBody.clientWidth);
	}
	//An error is raised if the IFrame domain != its container's domain
	catch(e)
	{
	window.status =	'Error: ' + e.number + '; ' + e.description;
	}
}
//-->
</SCRIPT>
</HEAD>
<BODY onload=reSize()>
	<iframe onresize=reSize() id=ifrm src=YOUR_PAGE_HERE></iframe>
</BODY>
</HTML>
				
This example uses try and catch to check for domain consistency, which are only available with Internet Explorer 5 and later. This error checking is included for illustration purposes and is not absolutely necessary; it only allows the script to fail gracefully.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

↑ Back to the top


References

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
167796 PRB: Permission Denied Error Message When Scripting Across Frame
For more information about measuring element dimensions and locations, see the following Microsoft Developer Network (MSDN) Web site: For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:

↑ Back to the top


Properties

Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

↑ Back to the top


Keywords: KB278469, kbsbnworkshop, kbhowto, kbdhtml

↑ Back to the top

Article Info
Article ID : 278469
Revision : 5
Created on : 4/28/2012
Published on : 4/28/2012
Exists online : False
Views : 369