Set iframe height to that of internal content (on same domain) - javascript

I have an iframe embedded within my page - it's just part of my page, not the whole thing. I am reloading the iframe dynamically and want it to resize according to the height of the content within it.
The content is on the same domain, so (hopefully?) no need for a solution as complex as that given in Resizing an iframe based on content.
This is the code I'm using to resize it, but it isn't working. The frame reloads OK, but the height always stays the same. Can anyone advise?
I've also tried the top solution in How to autosize an iframe without any luck.
I think it could be because the page inside the iframe takes a while to load, so it hasn't finished loading by the time the height is set. Maybe.
<iframe frameborder=0 border=0 src="" width="100%" height="400px" name="commentframe" id="commentframe"></iframe>
function reload_frame(uid) {
document.getElementById('commentframe').src = "comments.html?" + uid;
document.getElementById('commentframe').height = document.getElementById('commentframe').contentWindow.document.body.scrollHeight + "px";
}
Update:
Trying, with no luck:
<iframe frameborder=0 border=0 src="" width="100%" height="400px" name="commentframe" id="commentframe" onload="reload_frame()"></iframe>
function reload_frame() {
var commentframe = document.getElementById('commentframe');
commentframe.style.height = (commentframe.scrollHeight + 10).toString() + "px";
}
It does resize the iframe, but only to +10px of its original height - if the content is longer than this, it is still hidden.
Maybe I need an onload event inside the body of the iframe itself?

<script type="text/javascript">
function ajustHeight() {
$("#myIframe").parent()[0].style.height = ($("#myIframe")[0].scrollHeight + 150).toString() + "px";
}
</script>
<iframe id="myIframe" src="mypage.html" width="100%" height="100%" onload="ajustHeight()"></iframe>

In modern browsers and with many doctypes, scrollHeight will return the iframe height calculated by the browser and not the content height, so above snippets won't work in most cases.
You can set the iframe to the content height using the following snippet.
JS:
$('#ifm').load(function() {
$(this).height( $(this)[0].contentWindow.document.body.scrollHeight + 24);
});
HTML:
<iframe id="ifm src="..."></iframe>
Please note that this will work only if the iframe URL is on the exact same domain of the page containing the iframe itself.

without jquery:
<script type="text/javascript">
window.onload = function() {
var iframe = document.getElementById("blogFrame");
iframe.parentNode.style.height = iframe.scrollHeight + "px";
}
</script>

Related

iframe auto height without scroll doesn't work

I have a page that contains an iframe. I have tried to remove the scroll with the attribute scrolling=no and scroll='no' but without result.
So I want it to be able to adjust its height according to the contents inside it, without using scroll.
Here is my code:
<script>
function resizeIframe(obj) {
obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
}
</script>
<iframe src="" frameborder="0" onload="resizeIframe(this)" scrolling="no > </iframe>
I dont fully understand what you are trying to accomplish here and I presume the Iframe doesnt provide an API to handle this for you.
No scrolling:
Its a workaround but: fix this by wrapping the Iframe with a div, putting it on top of it with z-index. The cursor will not access the Iframe like this. Also want to be able to click it? Add an onclick function removing the diff or the css of the div.
Altering the height:
Just dont specify height and width and the IFrame will handle this for you?
About the code:
Im not familiar with the onload function but presuming it runs after the loading of the Iframe...
obj.style.height
should be
obj.height

Iframe height [dynamic] for every 2 second

my problem is when I load iframe it's set height of iframe correctly and also when data(content) load in iframe it;s automatically set height ,BUT when page load in iframe and set height relatively page content and then if I reduce or delete content from loaded page it will not reduce iframe height.(e.g when page load first time height set 100px and dynamically load data on page iframe height set to 150px and when I reduce data it's not set height of iframe it remain 150px). This is my code:
function setIframeHeight(iframe) {
if (iframe) {
var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
if (iframeWin.document.body) {
iframe.height = iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight;
}
setInterval("setIframeHeight_id('" + iframe.id + "')", 2000);
}
return false;
}
function setIframeHeight_id(iframeid) {
var iframe = document.getElementById(iframeid);
if (iframe) {
var iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow;
if (iframeWin.document.body) {
iframe.height = iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight;
}
}
return false;
}
function resizeIframe(nm) {
setIframeHeight(document.getElementById(nm));
}
HTML
<iframe id="IframeData" scrolling="no" frameborder="0" width="100%" onload="resizeIframe('IframeData')" allowtransparency="true"></iframe>
I would highly suggest you to use jQuery instead of plain JavaScript. It won't do a miracle by itself, but will sure help you shorten your code.
When it comes to your problem, why not using percentage sizes instead of fixed pixel size? Let's say your iframe's height is set to 100%, instead of 150px. By doing this way, next time you resize the parent container, the iframe itself will stretch to parent's height.
I hope this helps.

Iframe resize on content change

I have this code:
<iframe id='iFrame' src='iframe.php' onload='javascript:iFrameResize(this);'></iframe>
function iFrameResize(obj)
{
obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
}
It works fine when I first load the page. But, when I select something in the iframe and the content height is larger than the iframe height, the iframe does not resized.
Is there a way to always resize the iframe when it's content changes(the iframe location does not change, content is added/modified with javascript)?
Thanks in advance.

How to dynamically adjust iframe height of a Shindig Gadget

I want to dynamically adjust the height of an iframe of a Shindig Gadget depending on content inside of it. After some research I found out that it is required such iframe src to have <!DOCTYPE ...> declared to get the height of the content inside iframe using the following:
document.getElementById("iframe").contentWindow.document.body.scrollHeight
But it is impossible to define a doctype inside <![CDATA[ of a Shinding module. What's the best way to achieve this?
You should use the adjustHeight API in order to do this. Calling the API without any arguments will adjust the iFrames height to fit it's contents.
http://opensocial-resources.googlecode.com/svn/spec/trunk/Core-Gadget.xml#gadgets.window.adjustHeight
Try this
Here is an working example click here
<script type="text/javascript">
function resizeIframe(obj)
{
obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
}
</script>
<iframe src="http://www.yahoo.com" onload='javascript:resizeIframe(this);'></iframe>
</div>

How to auto-size an iFrame? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Resizing an iframe based on content
I'm loading an iFrame and want the parent to automatically change the height based upon the height of the iFrame's content.
To simply things, all pages belong to the same domain, so I shouldn't run into cross-site scripting issues.
On any other element, I would use the scrollHeight of the DOM object and set the height accordingly. I don't know if this would work on an iframe (because they're a bit kooky about everything) but it's certainly worth a try.
Edit: Having had a look around, the popular consensus is setting the height from within the iframe using the offsetHeight:
function setHeight() {
parent.document.getElementById('the-iframe-id').style.height = document['body'].offsetHeight + 'px';
}
And attach that to run with the iframe-body's onLoad event.
Try:
jquery-iframe-auto-height
iframe-resizer
I just happened to come by your question and i have a solution. But its in jquery. Its too simple.
$('iframe').contents().find('body').css({"min-height": "100", "overflow" : "hidden"});
setInterval( "$('iframe').height($('iframe').contents().find('body').height() + 20)", 1 );
There you go!
Cheers! :)
Edit: If you have a Rich Text Editor based on the iframe method and not the div method and want it to expand every new line then this code will do the needful.
Here is a dead simple solution that works on every browser and with cross domains:
First, this works on the concept that if the html page containing the iframe is set to a height of 100% and the iframe is styled using css to have a height of 100%, then css will automatically size everything to fit.
Here is the code:
<head>
<style type="text/css">
html {height:100%}
body {
margin:0;
height:100%;
overflow:hidden
}
</style>
</head>
<body>
<iframe allowtransparency=true frameborder=0 id=rf sandbox="allow-same-origin allow-forms allow-scripts" scrolling=auto src="http://www.externaldomain.com/" style="width:100%;height:100%"></iframe>
</body>
This solution worked best for me. It uses jQuery and the iframe's ".load" event.
In IE 5.5+, you can use the contentWindow property:
iframe.height = iframe.contentWindow.document.scrollHeight;
In Netscape 6 (assuming firefox as well), contentDocument property:
iframe.height = iframe.contentDocument.scrollHeight
I found the solution by #ShripadK most helpful, but it does not
work, if there is more than one iframe. My fix is:
function autoResizeIFrame() {
$('iframe').height(
function() {
return $(this).contents().find('body').height() + 20;
}
)
}
$('iframe').contents().find('body').css(
{"min-height": "100", "overflow" : "hidden"});
setTimeout(autoResizeIFrame, 2000);
setTimeout(autoResizeIFrame, 10000);
$('iframe').height($('iframe').contents().find('body').height() + 20) would set
the height of every frame to the same value, namely the height of the content of the first frame.
So I am using jquery's height() with a function instead of a value. That way the individual
heights are calculated
+ 20 is a hack to work around iframe scrollbar problems. The number must be bigger than the size of a scrollbar. The hack can probably
be avoided but disabling the scrollbars for the iframe.
I use setTimeout instead of setInterval(..., 1) to reduce CPU load in my case
My solution, (using jquery):
<iframe id="Iframe1" class="tabFrame" width="100%" height="100%" scrolling="no" src="http://samedomain" frameborder="0" >
</iframe>
<script type="text/javascript">
$(function () {
$('.tabFrame').load(function () {
var iframeContentWindow = this.contentWindow;
var height = iframeContentWindow.$(document).height();
this.style.height = height + 'px';
});
});
</script>
Oli has a solution that will work for me. For the record, the page inside my iFrame is rendered by javascript, so I'll need an infinitesimal delay before reporting back the offsetHeight. It looks like something along these lines:
$(document).ready(function(){
setTimeout(setHeight);
});
function setHeight() {
alert(document['body'].offsetHeight);
}
This is the easiest method i have found using prototype:
Main.html:
<html>
<head>
<script src="prototype.js"></script>
<script>
function init() {
var iframe = $(document.getElementById("iframe"));
var iframe_content = $(iframe.contentWindow.document.getElementById("iframe_content"));
var cy = iframe_content.getDimensions().height;
iframe.style.height = cy + "px";
}
</script>
</head>
<body onload="init()">
<iframe src="./content.html" id="iframe" frameBorder="0" scroll="no"></iframe>
<br>
this is the next line
</body>
</html>
content.html:
<html>
<head>
<script src="prototype.js"></script>
<style>
body {
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>
<div id="iframe_content" style="max-height:200px;">
Sub content<br>
Sub content<br>
...
...
...
</div>
</body>
</html>
This seems to work (so far) in all the major browsers.
My workaround is to set the iframe the height/width well over any anticipated source page size in CSS & the background property to transparent.
In the iframe set allow-transparency to true and scrolling to no.
The only thing visible will be whatever source file you use. It works in IE8, Firefox 3, & Safari.
Actually - Patrick's code sort of worked for me as well. The correct way to do it would be along the lines of this:
Note: there's a bit of jquery ahead:
if ($.browser.msie == false) {
var h = (document.getElementById("iframeID").contentDocument.body.offsetHeight);
} else {
var h = (document.getElementById("iframeID").Document.body.scrollHeight);
}

Categories