I'm attempting to send the height of my page from an iframe to the parent page using the postMessage API. The idea is that when the total page height of the iframed page changes, it will send that height to the parent. The parent will then adjust the iframe's height to match. I currently have this working great in the latest Google Chrome.
Unfortunately, I've run into a hiccup with IE9/10/11 and Firefox. Once the .scrollHeight has been set to its largest, it stays there. Attempting to send a smaller height just sends the previously highest height, even though the originating page's height is considerably less.
I've figured that it's probably because the iframe the page is being held in is affecting the originating page's height. So this may actually have a CSS related solution?
Here is a reduced test case I put together. This parent page contains an iframe that holds a page that can increase and decrease its own height using two buttons I added. Additionally, a third button grabs the pages height and sends it to the parent using the postMessage API.
Example Page: http://iaviglobal.com/parent.html
I've made it so that the height that is sent and received is logged in the console. Test the functionality by adding the orange block and sending the document height. Then hide the orange block and send the height again. Works great in the latest Chrome, not so much in IE and FF.
Any help would be greatly appreciated.
EDIT: I've determined that I can avoid this issue if I use the height of any element other than <html>
and <body>
. For instance, reporting the height of <div id="main">
will give an accurate number regardless of browser. <html>
and <body>
seem to report the full iframe height in IE and Firefox even if the content inside them don't fill that much space.
However, it would still be preferred if I could get the accurate <html>
or <body>
height instead of using a <div>
.