I'm trying to use an iframe to show content but the height of the content can vary and I want to try and elimate the vertical scroll bar. I know that you can have a variable height or an overflow in CSS or HTML. Is there a way I can do this using JavaScript?
How to auto size an iframe?
Collapse
X
-
Tags: None
-
Doesn't that just get the size of the browser window. I need to know how big the frame needs to be to fit all it's content. Is there a way to work that out?Comment
-
-
This will take care of it. It cannot work on cross-domain content.
Code:<iframe src='mypage.htm' id='if1' onload='setIframeHeight( this.id )' scrolling='no'></iframe> ...... <script type='text/javascript'> function setIframeHeight( iframeId ) { var ifDoc, ifRef = document.getElementById( iframeId ); try { ifDoc = ifRef.contentWindow.document.documentElement; } catch( e ) { try { ifDoc = ifRef.contentDocument.documentElement; } catch(ee){ } } if( ifDoc ) { ifRef.height = ifDoc.scrollHeight; // Enable / disable // ifRef.width = ifDoc.scrollWidth; // required lines } } </script>
Comment
-
I tried getting the height and putting it in the onload but I get permission denied. Is there someway of granting permission?Comment
-
If you have access to the pages that you are displaying in the iFrame then you can add some JavaScript that sets a Cookie to indicate the height and width of the page's content.
That way you can retrieve the height and width properties and set the iFrame's style accordingly in the parent window.
You cannot retrieve the contents of the iFrame using JavaScript for browser security reasons.
There is another thread on the topic that I helped with a while ago found here.
-FrinnyComment
-
If you have access to the pages that you are displaying in the iFrame then you can add some JavaScript that sets a Cookie to indicate the height and width of the page's content.
That way you can retrieve the height and width properties and set the iFrame's style accordingly in the parent window.Comment
-
Is there any other way of including content from one page in another page other than using an iFrame. I reason I wanted to know the height of the frame was to remove the scroll bars and make the page I'm including look like it's part of part of the parent.
BTW, I own both sites so I'm allowed to do this, they are just on different domains. Actually, is there anyway to get the dimensions if they're on the same server?Comment
-
To answer your 2nd question first, if they're on the same domain, it's easy. The suggested code samples should work.
If it's a different domain, you can serve it from the parent domain using a server-side proxy.
If you have control over the other domain, why can't you try setting it from within the iframe domain page?Comment
-
Would it make any difference if I used an object? Or do they have to have size set like with an iframe?
Is there any way to insert a page into a div that will autosize to contain it. If not, could someone give the non-IE equivilent to the object tags because I think only IE supports it.Comment
Comment