3

Hi I currently have 2 pages (index.html and iframe_contents.html). Both are on the same domain.

I am currently trying to get the iframe to dynamically resize based on the contents size.

I was using this to assist me http://benalman.com/code/projects/jquery-resize/examples/resize/ and it works if the iframe_contents body tag gets larger or smaller on Firefox and IE 7/8/9 but for webkit it only can grow and can never shrink

I've narrowed it down to the body tag in iframe_contents.html not shrinking when content height changes but only in the iframe. When iframe_contents.html is not in a iframe if I shrink / enlarge elements the bodies overall height changes.

Is this a webkit specific issue?

NegiToro
  • 31
  • 1
  • 3
  • can you post your code to [jsfiddle](http://jsfiddle.net)? The example site you gave works for me in Chrome (webkit) – Eonasdan Aug 30 '11 at 20:00
  • it works for growing but not for shrinking but yes I'll post to the website js fiddle – NegiToro Aug 30 '11 at 20:02
  • ah! sorry, didn't read the question correctly. – Eonasdan Aug 30 '11 at 20:03
  • here is the code - http://jsfiddle.net/LKCbq/ the index is the first bit and the iframe contents is the second. I also provided a link to the js file which I am using as well – NegiToro Aug 30 '11 at 20:04

2 Answers2

4

After reading lots of answers here they all had the same issue with not resizing smaller when needed. I think most people are just doing a one-off resizing after the frame loads, so maybe don't care. I need to resize again anytime the window size changes. So for me, if they made the window narrow the iframe would get very tall, then when they make the window larger it should get shorter again. This wasn't happening on some browsers because the scrollHeight, clientHeight, jquery height() and any other height I could find with DOM inspectors (FireBug/Chrome Dev Tools) did not report the body or html height as being shorter after the iframe was made wider. Like the body had min-height 100% set or something.

For me the solution was to make the iframe 0 height, then check the scrollHeight, then set to that value. To avoid the scrollbar on my page jumping around, I set the height of the parent (that contains the iframe) to the iframe height to keep the total page size fixed while doing this.

I wish I had a cleaner sample, but here is the code I have:

        $(element).parent().height($(element).height());
        $(element).height(0);
        $(element).height($(element).contents().height());
        $(element).parent().height("");

element is my iframe.

The iframe has width: 100% style set and is inside a div with default styles (block).

Code is jquery, and sets the div height to the iframe height, then sets iframe to 0 height, then sets iframe to the contents height. If I remove the line that sets the iframe to 0 height, the iframe will get larger when needed, but never smaller.

eselk
  • 6,764
  • 7
  • 60
  • 93
1

This may not help you much but here is a function we have in what would be your iframe_contents.html page. It will attempt to resize the iframe in which it is loaded in a sort of self-resizing, cross-browserish, pure-JavaScript kind of way:

function makeMeFit() {
    if (top.location == document.location) return; // if we're not in an iframe then don't do anything
    if (!window.opera && !document.mimeType && document.all && document.getElementById) {
        parent.document.getElementById('youriframeid').style.height = (this.document.body.offsetHeight + 30) + "px";
    } else if (document.getElementById) {
        parent.document.getElementById('youriframeid').style.height = (this.document.body.scrollHeight + 30) + "px"
    }
}

You could put calls to it in a resize() event or following an event that changes the height of your page. The feature-testing in that method should separate out WebKit browsers and pick the correct height property.

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
  • The problem with this is that I need the parent to be the one handling everything. There are a lot of contents in the iframe that could possibly change and wouldn't be good to handle. The major issue is that it only doesn't resize when the content shrinks.. growing is okay which is odd – NegiToro Aug 30 '11 at 20:08