Enrico
Enrico

Reputation: 91

Unable to use postMessage within cordova

In our cordova/phonegap-app we're using an iFrame to load external websites. To communicate with the iFrame-content I've created a script for both sites (App and Website) which uses iFrame.contentWindow.postMessage()-method. The reason is to open external links within the iframe in the system-browser of the smartphone. This solution worked fine on cordova 2.3.0.

Today I've updated the cordova version of the app to 3.1.0 and deleted some permissions from android-manifest. But the communication with the iFrame-content doesn't work. Following error error-message is shown:

"Unable to post message to https://[domain]. Recipient has origin file://"

To post a message to the IFrame I use following code:

$('#webViewIFrame').get(0).contentWindow.postMessage('message', host);

Has anybody an idea why my communication-solution doesn't work with cordova 3.1.0?

Upvotes: 5

Views: 3532

Answers (2)

rafahoro
rafahoro

Reputation: 1257

You will need to use:

$('#webViewIFrame').get(0).contentWindow.postMessage('message',"*");

Since phonegap/cordova pages are served at "file://" and according to https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

"...posting a message to a page at a file: URL currently requires that the targetOrigin argument be "*". file:// cannot be used as a security restriction; this restriction may be modified in the future."

Upvotes: 0

Miquel
Miquel

Reputation: 8989

Normally this should be because of cross domain problem. Try using this:

$('#webViewIFrame').get(0).contentWindow.postMessage('message', '*');

Upvotes: 2

Related Questions