1
votes

I have iframe with content like below,

    <iframe  frameborder="no" src="http:localhost/com" id="iframe">
     <div style="height:850px;width:700px;">div text </div>
   </iframe>

This shows the iframe with hori. and vertical scroll but not the height on inner content of 850px.

how can i change the iframe height dynamiclly based on inner content height n width. Im using jquery inside the content.

Its cross domain js widget so most of the below answers throwing permission denied error

Thanxs, Nithish

5
possible duplicate of How to auto-size an iFrame? Also, check this one.Nikita Rybak
One cross-domain solution is described in my second link. But imho, it's better to get rid of iframe and switch to script tag.Nikita Rybak

5 Answers

2
votes

Nithish,

if you are using jquery, then you can easily find (and set) the height of any div (in your domain - not on external content) as such:

// get the height - in case we want to factor it into the equation below!!
var myDivHeight = $('iframe').css('height');

// set the height
var docHeight = $(document).css('height');
$('iframe').css('height', docHeight);

hope this helps ...

2
votes
var newHeight = $("iframe[id='iframeAutenticador']").contents().find("html").height();
$("iframe[id='iframeAutenticador']").height(newHeight);

use this.

1
votes

You should create a function to measure the height of the content, and set the IFrame height and then call the resize function when the content is loaded.

<SCRIPT LANGUAGE="JavaScript">
function resizeIframeToFitContent(iframe) {
    // This function resizes an IFrame object
    // to fit its content.
    // The IFrame tag must have a unique ID attribute.
    iframe.height = document.frames[iframe.id]
                    .document.body.scrollHeight;
}
</SCRIPT>
0
votes

If domains are the same for both pages:

parent.document.getElementById("iframe").style.height = $(document).height()+"px";

You can use jQuery in parent page too (if its available there).

0
votes

I found this here: stackoverflow.com/questions/1145850/

and expanded upon it to re-size width also...you just add this in your head:

    <script type="text/javascript">
        function getDocHeight(doc) {
            doc = doc || document;
            // stackoverflow.com/questions/1145850/
            var body = doc.body, html = doc.documentElement;
            var height = Math.max( body.scrollHeight, body.offsetHeight, 
                html.clientHeight, html.scrollHeight, html.offsetHeight );
            return height;
        }
        function getDocWidth(doc) {
            doc = doc || document;
            // stackoverflow.com/questions/1145850/
            var body = doc.body, html = doc.documentElement;
            var width = Math.max( body.scrollWidth, body.offsetWidth,
                html.clientWidth, html.scrollWidth, html.offsetWidth );
            return width;
        }
        function setIframeSize(id) {
            var ifrm = document.getElementById(id);
            var doc = ifrm.contentDocument? ifrm.contentDocument: 
                ifrm.contentWindow.document;
            ifrm.style.visibility = 'hidden';
            ifrm.style.height = "10px"; // reset to minimal height ...
            ifrm.style.width = "10px"; // reset to minimal width ...
            // IE opt. for bing/msn needs a bit added or scrollbar appears
            ifrm.style.height = getDocHeight( doc ) + 4 + "px";
            ifrm.style.width = getDocWidth( doc ) + 4 + "px";
            ifrm.style.visibility = 'visible';
        }
    </script>

Then make sure you have a unique id in your iframe and call the SetIframeSize:

<iframe src="source.html" id="uniqueid" onload="setIframeSize(this.id)"></iframe>