我有一个iframe,其中有一个JavaScript函数:
function callParent(){
parent.postMessage('closeModal','*');
return true;
}
我主页上的其他JavaScript函数:
$(window).on('message',function(e){
console.log(e);
});
它正在打印e,但在e.data中却给出了undefined。我期望数据将是一个字符串'closeModal'。
如何从iframe获取消息字符串?
您正在传递的数据在
e.originalEvent.data
所以在主页上:
$(window).on('message',function(e){
console.log(e.originalEvent.data);
});
如前所述,Pointy
jQuery创建了自己的事件对象。
如果您不使用jQuery,那您将不得不
window.onmessage = function(e){
console.log(e.data);
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句