Getting the height of a frame in javascript

Marcus Frödin picture Marcus Frödin · Sep 7, 2010 · Viewed 11.6k times · Source

I'm currently working in a system with a bit of a legacy presentation layer -- it uses frames (actual, in a frameset frames).

To get some event handling up and running I would like to get events when the frame resizes or at least be able to measure the height of the frame somehow. There seems to be no cross browser way to do this. Has anyone tried anything like this?

Answer

Sachin Shanbhag picture Sachin Shanbhag · Sep 7, 2010

Try this - Also you might have to try this on all browsers too -

<!-- 
     Example File From "JavaScript and DHTML Cookbook"
     Published by O'Reilly & Associates
     Copyright 2003 Danny Goodman
-->


function getFrameSize(frameID) {
    var result = {height:0, width:0};
    if (document.getElementById) {
        var frame = parent.document.getElementById(frameID);
        if (frame.scrollWidth) {
            result.height = frame.scrollHeight;
            result.width = frame.scrollWidth;
        }
    }
    return result;
}