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.
Asked
Active
Viewed 1.9k times
8
-
does anybody still use frames? – yoda Feb 09 '11 at 01:22
-
6@yoda he meant iframe, and they are only used by insignificant companies in the websphere like Google and Facebook. – Itay Moav -Malimovka Feb 09 '11 at 01:45
-
the framed content is in your site? – Trufa Feb 09 '11 at 01:45
-
possible duplicate of [Resizing an iframe based on content](http://stackoverflow.com/questions/153152/resizing-an-iframe-based-on-content) – Pablo Fernandez Feb 09 '11 at 01:52
-
1You've seen this GitHub project? https://github.com/house9/jquery-iframe-auto-height – jpwco Feb 09 '11 at 01:55
4 Answers
1
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;

Val Schuman
- 1,798
- 2
- 18
- 23
-
3this does not work. You can't access the iframe div from outside the iframe – Pablo Fernandez Feb 09 '11 at 01:53
-
4You can, however, do this: `document.getElementById("frameId").contentWindow.document.getElementById("id")` – JCOC611 Feb 09 '11 at 01:56
-
@JCOC611 `document.getElementById("frameId").contentWindow.document` is forbidden to access these days, too, as far as I can see (at least in FF) – YakovL Nov 14 '16 at 23:03
-
@YakovL it looks like it works fine in Chrome 56.0. Make sure the main document and the iframe are on the same domain. – JCOC611 Nov 14 '16 at 23:13
-
@JCOC611 sorry, either missed the "Not cross-domain." part in the question or commented before figured that it doesn't work only cross-domain. There's this answer to be noted, though: http://stackoverflow.com/questions/153152/resizing-an-iframe-based-on-content/362564#362564 – YakovL Nov 16 '16 at 11:41
0
Here's a solution using jQuery:
$('#iframe').height($('#iframe').contents().find('body').height());

Chun
- 2,230
- 5
- 24
- 46

Erik Bergstedt
- 912
- 10
- 27
0
<iframe id="moo" src="something.html"><iframe>
<script>
function onContentChange(){
var NewSize=$('moo').getScrollSize();
$('moo').setStyles({
width: NewSize.x,
height: NewSize.y
});
}
</script>
and inside the somthing.html, on the bottom or onload event do something on the lines of
window.parent.window.onContentChange()

Itay Moav -Malimovka
- 52,579
- 61
- 190
- 278
0
Render dynamically the iframe in javascript once the container element("td1") has been drawn.
<script type="text/javascript">
var iframesrc = "@Href("~/about")";
function init() {
var vHeight = document.getElementById("td1").offsetHeight;
document.getElementById("td1").innerHTML="<iframe style=\"width:100%; height:" + vHeight + "px\" src=\"" + iframesrc + "\"></iframe>";
}
window.onload = init;
</script>
...
<td id="td1" style="width: 100%; height:100%;">
</td>

Luis
- 1