Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

Calculating the height of an iframe

Hi,
I am using the following code to determine the height of the contents an iframe. The issue is that it works on chrome.. But has random behaviour on safari :
document.addEventListener('DOMContentLoaded', function () {
    var body = document.body, html = document.documentElement;
    var height = Math.max(body.scrollHeight, body.offsetHeight,
                          html.clientHeight, html.scrollHeight, html.offsetHeight);
    window.parent.postMessage({
        name: 'resizeAttachmentView',
        iframeId: body.dataset.iframeId,
        height: height
    }, '*');
});

Open in new window


What could be the issue.. I see sometimes whitespace below the iframe. The same iframe one time the height is calculated on rendering as 40px and some other random time as 140 px.

What could be the reason for this. This is happening only on safari on chrome its working fine..
This iframe is basically rendered inside my web application and i set the height properly using the above code after it has rendered.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial