Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
124
rated 0 times [  130] [ 6]  / answers: 1 / hits: 19427  / 14 Years ago, wed, february 9, 2011, 12:00:00

How would I make an iframe's height adjust to the content within it? I assume you'd calculate the difference between the content height and iframe height (something I don't know how to do) and use that with a while loop to increment a variable to use as a height, but I can't wrap my head around how to actually do these things. Before asking: yes, the content in the frame is from my site. Not cross-domain.


More From » html

 Answers
277

Not quite sure why you want to use an iframe and not a dynamic div filled, say, via ajax, but:



Put the content within the iframe into a div, then get the height of the div. I'd suggest using jquery like so:



$(#divId).height();


But if you can't use jquery, you should be able to use this:



document.getElementById(divId).offsetHeight;


Then you'd need to set the iframe's height to whatever you got.



jquery:



$(#iframeId).height($(#divId).height());


regular js:



document.getElementById(iframeId).style.height = 
document.getElementById(divId).offsetHeight;

[#93827] Monday, February 7, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mireyag

Total Points: 73
Total Questions: 107
Total Answers: 85

Location: Ukraine
Member since Sun, Dec 13, 2020
3 Years ago
mireyag questions
Sun, Aug 15, 21, 00:00, 3 Years ago
Wed, Dec 16, 20, 00:00, 3 Years ago
Tue, Sep 1, 20, 00:00, 4 Years ago
Sun, Jul 5, 20, 00:00, 4 Years ago
;