开发者

How do I resize an iframe based on its content?

I would like iframe to be big enough to not have scrollbars, and if needed, require the browser render the scrollbars.

There have been posts bef开发者_JS百科ore, i know... but .. many weren't quite my problem.

The main page and the iframe are on the same domain.


<html>
<script type="text/javascript">
function autoIframe(frameId){
try{
frame = document.getElementById(frameId);
innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
objToResize = (frame.style) ? frame.style : frame;
objToResize.height = innerDoc.body.scrollHeight + 10;
}
catch(err){
window.status = err.message;
}
}
</script>



<iframe id="tree" name="tree" src="tree.htm" onload="if (window.parent && window.parent.autoIframe) {window.parent.autoIframe('tree');}"></iframe>

thats a script i had saved in my temlates, it isnt mine though! but i cant remember who was author..

doing a search on google returned:

<script language="JavaScript">

function calcHeight()
{
  //find the height of the internal page
  var the_height=
    document.getElementById('the_iframe').contentWindow.
      document.body.scrollHeight;

  //change the height of the iframe
  document.getElementById('the_iframe').height=
      the_height;
    }
<iframe name="the_iframe" onLoad="calcHeight();" scrolling="no" width="730"  id="the_iframe" src="you_page.html" frameborder="0" allowtransparency="true"></iframe>


You can access the contents of an iFrame if they are on the same domain:

http://roneiv.wordpress.com/2008/01/18/get-the-content-of-an-iframe-in-javascript-crossbrowser-solution-for-both-ie-and-firefox/

Doing that you should be able to get some sort of height/width/offset value from the contents and adjust the size of the iframe appropriately.

Of course, if they are indeed on the same domain, perhaps an AJAX call into a DIV would make more sense.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜