postMessage在PhoneGap不工作 - iframe到父消息 [英] postMessage in PhoneGap not working - iframe to parent messaging
问题描述
我已经构建了一个PhoneGap应用程序,它使用与应用程序捆绑在一起的iframe,我试图传递e消息从iframe到父,似乎没有工作,当我运行在实际的iPad上的应用程序;然而,它运行正常,当我在同一台设备上的浏览器中运行应用程序。
I've build a PhoneGap app which which makes use of an iframe which is bundled with the app and I'm am trying to pass e message from the iframe to the parent which doesn't seem to be working when I run the app on an actual iPad; however it works fine when I run the app in the browser on the same device.
这里是我在iframe中使用的代码来发送消息,注意我使用HammerJS来捕获一些事件:
Here is the code I'm using inside the iframe to send a message, note that I'm using HammerJS to capture some events:
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
});
以及我用来取得邮件的代码:
and the code I'm using to get the message:
window.addEventListener('message',function(event) {
alert(event.data);
},false);
推荐答案
作为域名,因此代码将如下所示:
And the answer is to use "file://" as the domain name so the code will look like this:
var domain = 'file://';
$('body').hammer().on("swipe", "", function(event) {
var message = event.gesture.direction;
parent.postMessage(message,domain); //send the message and target URI
});
这篇关于postMessage在PhoneGap不工作 - iframe到父消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!