Blog   About   Contact

IE auto content fit IFrame HACK

I ran into 'another' strange IE-browser behavior yesterday. It took me an hour of two to solve this iframe-height-problem.

If you call this.document.body.scrollHeight in an iframe twice, the first value is wrong, but the second value is okay.

Look at the example below:


    function autoFitIFrame(elementId){
        if (!window.opera && !document.mimeType && document.all && document.getElementById){
            parent.document.getElementById(elementId).style.height=this.document.body.offsetHeight+"px";
        }
        else if(document.getElementById) {
            // ErnestoZ HACK: if you call scrollHeight TWICE, the second value is the real height of the iframe
            var scrollHeightWrong = this.document.body.scrollHeight;
            var scrollHeight = this.document.body.scrollHeight;
            alert(scrollHeightWrong);
            alert(scrollHeight);
            parent.document.getElementById(elementId).style.height = scrollHeight+"px";
        }
    }

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.9.004.