PhoneGap中的postMessage不起作用-iframe到父消息
postMessage in PhoneGap not working - iframe to parent messaging
我已经构建了一个PhoneGap应用程序,它利用了与该应用程序捆绑在一起的iframe,我正试图从iframe向父级传递电子邮件,当我在实际的iPad上运行该应用程序时,该消息似乎不起作用;然而,当我在同一台设备上的浏览器中运行该应用程序时,它运行良好。
以下是我在iframe中用来发送消息的代码,请注意,我正在使用HammerJS来捕获一些事件:
var domain = 'http://' + document.domain;
$('body').hammer().on("swipe", "", function(event) {
var message = event.gesture.direction;
parent.postMessage(message,domain); //send the message and target URI
});
以及我用来获取信息的代码:
window.addEventListener('message',function(event) {
alert(event.data);
},false);
答案是使用"file://"作为域名,因此代码看起来像这样:
var domain = 'file://';
$('body').hammer().on("swipe", "", function(event) {
var message = event.gesture.direction;
parent.postMessage(message,domain); //send the message and target URI
});
尝试使用
var domain = '*';
通常情况下,这应该是因为跨域问题,请参阅此处的更多信息
您需要使用:
parent.postMessage(message,"*");
由于phonegap/cordova页面以"file://"提供,因此根据https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
"…将消息发布到文件中的页面:URL当前要求targetOrigin参数为"*"。file://不能用作安全限制;将来可能会修改此限制。"
相关文章:
- 如何检查消息(postMessage)来自哪个跨域iframe
- Fine Uploader-未收到来自已加载iframe的有效消息
- 谷歌地图Api在CRM Dynamics 2011 iframe"不可能使用显示属性“;消息
- 将消息从主 http 页面发送到 https iframe 时的安全注意事项
- 检测哪个 iframe 发送了开机自检消息
- HTML5 在父脚本中为 iframe 设置消息处理程序
- 我可以在Iframe上显示console.log消息吗
- 有没有一种方法可以在加载页面后检查iframe是否有错误消息
- PHP-收到新消息时更新iFrame
- window.parent.postMessage父级检查iframe发送的消息
- 正在尝试从YouTube捕获console.log消息<iframe>
- 将自定义消息从iframe来宾发送到iframe主机,其中托管portlet中的HTML
- PhoneGap中的postMessage不起作用-iframe到父消息
- 两个窗口或选项卡之间的跨文档消息传递,而不是iframe
- 如何在iFrame加载时显示加载消息
- 在弹出框中从iframe发送消息到后台脚本
- IFrame和IFrame之间的跨文档消息传递问题家长
- Youtube iframe API发布消息失败
- 从iFrame跨所有浏览器传递消息
- 在加载javascript iframe时加载消息