Javascript 增补列表器';s回调函数未被正确调用
我使用Postmessage从我的页面中的iframe(跨域)获取正文内容的高度。下面是我在父页面中编写的代码,我编写了console.log来检查handleMessage函数的值Javascript 增补列表器';s回调函数未被正确调用,javascript,settimeout,addeventlistener,Javascript,Settimeout,Addeventlistener,我使用Postmessage从我的页面中的iframe(跨域)获取正文内容的高度。下面是我在父页面中编写的代码,我编写了console.log来检查handleMessage函数的值 var addEvent = window.attachEvent||window.addEventListener; var event = window.attachEvent ? 'onmessage' : 'message'; addEvent(event, handleMessa
var addEvent = window.attachEvent||window.addEventListener;
var event = window.attachEvent ? 'onmessage' : 'message';
addEvent(event, handleMessage);
function handleMessage(e) {
var targetHeight = e.data;
targetHeight = parseInt(e.data);
console.log(targetHeight);
$('.wrapper').height(targetHeight+'px');
}
我用于在子站点中发布高度的脚本如下所示:
$(document).ready(function(){
$(window).bind("resize", resizeWindow);
resizeWindow();
});
function resizeWindow(){
var body = document.body,
html = document.documentElement,
height = body.offsetHeight;
if(height === 0){
height = html.offsetHeight;
}
// * can be replaced with the live URL in production.
parent.postMessage(height, '*');
}
我页面中的子站点在页面加载中发布了四次高度,但当我检查控制台时,它只显示两个值
但当我在handleMessage函数中执行警报(或settimeout())时,我可以在控制台中看到4个值。
你能告诉我为什么我的函数没有被调用4次吗
提前感谢。您可以创建更完整的示例?感谢您的回复。我已经编辑了我的文章,并就我的问题提供了更多信息。你的例子仍然没有帮助。如何以及何时打4次电话?