2

I'm trying to workout the height of an iFrame and can not understand why

document.body.offsetHeight + document.body.bottomMargin 

does not equal

document.documentElement.offsetHeight

when all other margins are set to zero and the bottom margin has a value below 16px.

Once the bottom margin is more than 16px the above two values equal each other in FireFox and are within 1px in Chrome.

Strangely this problem doesn't effect the width calculation.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
David Bradshaw
  • 11,859
  • 3
  • 41
  • 70

1 Answers1

0

After much digging around I came up with this to solve the problem.

function getIFrameHeight(){
    function getComputedBodyStyle(prop) {
        return parseInt(
            document.defaultView.getComputedStyle(document.body, null),
            10
        );
    }

    return document.body.offsetHeight +
        getComputedBodyStyle('marginTop') +
        getComputedBodyStyle('marginBottom');
}

and an expanded version for IE8 and below.

function getIFrameHeight(){
    function getComputedBodyStyle(prop) {
        function getPixelValue(value) {
            var PIXEL = /^\d+(px)?$/i;

            if (PIXEL.test(value)) {
                return parseInt(value,base);
            }

            var 
                style = el.style.left,
                runtimeStyle = el.runtimeStyle.left;

            el.runtimeStyle.left = el.currentStyle.left;
            el.style.left = value || 0;
            value = el.style.pixelLeft;
            el.style.left = style;
            el.runtimeStyle.left = runtimeStyle;

            return value;
        }

        var 
            el = document.body,
            retVal = 0;

        if (document.defaultView && document.defaultView.getComputedStyle) {
            retVal =  document.defaultView.getComputedStyle(el, null)[prop];
        } else {//IE8 & below
            retVal =  getPixelValue(el.currentStyle[prop]);
        } 

        return parseInt(retVal,10);
    }

    return document.body.offsetHeight +
        getComputedBodyStyle('marginTop') +
        getComputedBodyStyle('marginBottom');
}
David Bradshaw
  • 11,859
  • 3
  • 41
  • 70