ReceiveMessage事件显示数据未定义

里特什·梅汉迪拉塔(Ritesh Mehandiratta)

我有一个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);
});

如前所述,PointyjQuery创建了自己的事件对象。

如果您不使用jQuery,那您将不得不

window.onmessage = function(e){
    console.log(e.data);
};

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章