DelphiFAQ Home Search:

Adjusting the height of an IFRAME according to its content

 

comments18 comments. Current rating: 4 stars (4 votes). Leave comments and/ or rate it.

Question:

On my page I display search results in an IFRAME. Sometimes there's only 5 rows of data, sometimes it can be 2000. I need to have the IFRAME size adjust to the number of elements. I was working with a construct like
iframe_1.height = 30 + 16 * (number_of_rows)

but this estimating formula relies on a certain font size. And it looks really wrong in Netscape or Opera.

Answer:

Why estimate if you can make an exact calculation of the document height? Take the code below which will work on Internet Explorer and various Netscape versions.

<script type="text/javascript">
function adjustIFrameSize (iframeWindow) {
  if (iframeWindow.document.height) {
    var iframeElement = parent.document.getElementById(iframeWindow.name);
    iframeElement.style.height = iframeWindow.document.height + 'px';
    iframeElement.style.width = iframeWindow.document.width + 'px';
  }
  else if (document.all) {
    var iframeElement = parent.document.all[iframeWindow.name];
    if (iframeWindow.document.compatMode &&
        iframeWindow.document.compatMode != 'BackCompat') 
    {
      iframeElement.style.height = 
        iframeWindow.document.documentElement.scrollHeight + 5 + 'px';
      iframeElement.style.width = 
        iframeWindow.document.documentElement.scrollWidth + 5 + 'px';
    }
    else {
      iframeElement.style.height = iframeWindow.document.body.scrollHeight + 5 + 'px';
      iframeElement.style.width = iframeWindow.document.body.scrollWidth + 5 + 'px';
    }
  }
}
</script>

Content-type: text/html

Comments:

You are on page 2 of 2, other pages: 1 [2]
2012-07-23, 22:42:43
anonymous from United States  
2015-02-01, 03:59:17
anonymous  
If not for your writing this topic could be very coolevutnd and oblique.
2015-04-08, 14:08:21
hleablf  
rating
dzcyqvpo
You are on page 2 of 2, other pages: 1 [2]

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: