After extensive research it appears that this should work, but in IE8 the letsgo function never gets called... any help?
<script type="text/javascript">
function resizeCrossDomainIframe() {
if (window.addEventListener) {
window.addEventListener('message', letsgo, false);
} else if (window.attachEvent) {
window.attachEvent('onmessage', letsgo);
}
}
function letsgo(event) {
var iframe = document.getElementById('my_iframe');
if (event.origin !== '') return; // only accept messages from the specified domain
if (isNaN(event.data)) return; // only accept something which can be parsed as a number
var height = parseInt(event.data) + 32; // add some extra height to avoid scrollbar
iframe.height = height + "px";
}
</script>
<iframe src='.aspx?iframe=true&partnerid=222&site=localhost:62014' frameborder="0" width="100%" scrolling="auto" style="min-height: 750px; min-width: 600px; background-color: #fff;" id="my_iframe" onload="resizeCrossDomainIframe();">
</iframe>
After extensive research it appears that this should work, but in IE8 the letsgo function never gets called... any help?
<script type="text/javascript">
function resizeCrossDomainIframe() {
if (window.addEventListener) {
window.addEventListener('message', letsgo, false);
} else if (window.attachEvent) {
window.attachEvent('onmessage', letsgo);
}
}
function letsgo(event) {
var iframe = document.getElementById('my_iframe');
if (event.origin !== 'http://mysite.') return; // only accept messages from the specified domain
if (isNaN(event.data)) return; // only accept something which can be parsed as a number
var height = parseInt(event.data) + 32; // add some extra height to avoid scrollbar
iframe.height = height + "px";
}
</script>
<iframe src='http://mysite./products/default.aspx?iframe=true&partnerid=222&site=localhost:62014' frameborder="0" width="100%" scrolling="auto" style="min-height: 750px; min-width: 600px; background-color: #fff;" id="my_iframe" onload="resizeCrossDomainIframe();">
</iframe>
Share
Improve this question
edited Feb 5, 2013 at 17:54
SirM
asked Feb 5, 2013 at 17:27
SirMSirM
4971 gold badge6 silver badges22 bronze badges
3
-
You have
<!DOCTYPE html>
as the very first line in your page? – Teemu Commented Feb 5, 2013 at 18:10 - Yes I do... I guess i should add that this works great in FF, Chrome and IE9. Also the child iframe page is calling postMessage like so: <body onload="window.parent.postMessage(document.body.scrollHeight, 'http://" + Session["iframe_site"] + "');"> – SirM Commented Feb 5, 2013 at 18:28
-
It seems that in every example code at MSDN there's
event.domain
instead ofevent.origin
, althought it's not the first time when encounting errors within examples at MSDN. – Teemu Commented Feb 5, 2013 at 19:37
1 Answer
Reset to default 7I got it, must have been a race condition. I took out the onload.
<script type="text/javascript">
if (window.addEventListener) {
window.addEventListener('message', letsgo, false);
} else if (window.attachEvent) {
window.attachEvent('onmessage', letsgo);
}
function letsgo(event) {
var iframe = document.getElementById('my_iframe');
if (event.origin !== 'http://mysite.') return; // only accept messages from the specified domain
if (isNaN(event.data)) return; // only accept something which can be parsed as a number
var height = parseInt(event.data) + 32; // add some extra height to avoid scrollbar
iframe.height = height + "px";
}
</script>
<iframe src='http://mysite./products/default.aspx?iframe=true&partnerid=222&site=localhost:62014' frameborder="0" width="100%" scrolling="auto" style="min-height: 750px; min-width: 600px; background-color: #fff;" id="my_iframe" >
</iframe>