EDIT 2:
This isn't meant to answer the issue of Chrome/Firefox/etc onload differences (chrome onload fires before images are loaded giving the smaller frame height) but if you were already getting around that with your own code you can do this to display the modal with the proper height.
JS
// edited old function
function receiveMessage(event) {
if (event.origin !== "http://example.com") return;
$('#modal').css('height', parseInt(event.data, 10) + 10 + 'px');
$('#iframe').css('height', parseInt(event.data, 10) + 'px');
}
// new code
$('#show').click(function(e) {
// use if CSS hides modal initially
$('#modal').css('display', 'block');
// use if hidden with inline style
$('#modal').show();
$('#iframe').attr('src', $('#iframe').data('src'));
});
HTML
<div id="modal" style="display: none;"><!-- for inline styling -->
<iframe data-src="http://example.com" src="" id="iframe">
</iframe>
</div>
<button id="show">Open the modal</button>
GLHF
EDIT:
(I deleted the original part of the answer now that I know your requirements)
Still got the inspiration from here though.
Take #2:
I happen to run the same server under multiple domains and I finally have a use for it (yay).
So the parent page is on a different domain than the child page.
The parent page is only allowed this re-sizing madness
if it's own origin matches what the child passes as an argument in postMessage();
- thus eliminating those darn leachers who want to re-size like us big kids.
The child page is only allowed to be re-sized if it comes from a hood near you (specified in receiveMessage()
as a conditional) - also known as matching the origin
This is what you already knew but since I'm slow (and surely other Googlers in the future will be as well), I'm spelling it out here for us.
Here's my corrected code:
Parent:
var addEvent = function (element, myEvent, fnc) {
return ((element.attachEvent) ? element.attachEvent('on' + myEvent, fnc) : element.addEventListener(myEvent, fnc, false));
};
addEvent(window, "message", receiveMessage);
function receiveMessage(event) {
if (event.origin !== "http://www.myChildPageInsideMe.com") return;
$('#iframe').css('height', parseInt(event.data, 10) + 10 + 'px');
}
Child / iFrizzame:
<body onload="parent.postMessage(document.body.scrollHeight, 'http://www.theOverBearingMotherPage.com');">
Feel free to use whatever event handling code you like. I like mine :)
And of course...