我有一个iframe,其中有一个JavaScript函数:
function callParent(){
parent.postMessage('closeModal','*');
return true;
}
我主页中的其他JavaScript函数:
$(window).on('message',function(e){
console.log(e);
});
它正在打印e但是在e.data中它是未定义的。我期待数据将是一个字符串'closeModal'。
如何从iframe获取消息字符串?
答案 0 :(得分:0)
您传递的数据在
中 e.originalEvent.data
在主页面上:
$(window).on('message',function(e){
console.log(e.originalEvent.data);
});
当Pointy
注释时,jQuery会创建自己的事件对象。
如果您没有使用jQuery,那么
window.onmessage = function(e){
console.log(e.data);
};