function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Steven FafelSteven Fafel 

IFrame dynamic height in view page

Hello all.  We are looking to a solution for a height problem.  We have an IFrame on a detail page (that contains a Visualforce page).  The height of the content of the IFrame is dynamic but it seemed like our only choice was to set a fixed height on the IFrame.  When the content doesn't fill the frame height, the result is a blank space.  The image below (or attached) shows this blank space.  Does anyone know how to set the height of the IFrame based on the height of the content?

 IFrame Dynamic Height Needed
kevin lamkevin lam
Add this script to your Visualforce page:

<script>
    // Resize the iframe that contains the Visualforce page
    function resizeIframe() {
        var frameHeight = document.body.scrollHeight;
        var iframeElements = parent.document.getElementsByTagName("iframe");
        for (var i = 0; i < iframeElements.length; i++){
            if (iframeElements[i].title == '{!$CurrentPage.Name}'){
                var iframeElement = parent.document.getElementById(iframeElements[i].id);
                if (iframeElement) {
                    iframeElement.style.height = (frameHeight)+"px";
                }
            }
        }
    }

    if (document.addEventListener) {
        checkIfReady = function() {
            document.removeEventListener("DOMContentLoaded", checkIfReady, false);
            resizeIframe();
        };
    }
    else if (document.attachEvent) {
        checkIfReady = function() {
            if (parent.document.readyState == "complete") {
                parent.document.detachEvent("onreadystatechange", checkIfReady );
                resizeIframe();
            }
        };
    }

    if (document.addEventListener) {
        document.addEventListener("DOMContentLoaded", checkIfReady, false);
    }
    else if (document.attachEvent) {
        parent.document.attachEvent("onreadystatechange", checkIfReady);
    }
</script>
Peter Malmborg 1Peter Malmborg 1
Hi Kevin,
Does your suggestion really work? I get a cross site scripting error when I try it out:
Uncaught SecurityError: Blocked a frame with origin "https://c.cs87.visual.force.com" from accessing a frame with origin "https://cs87.salesforce.com"

Perhaps this workaround has been blocked in the Winter '16 release?

BR,
  Peter