0

I have a program where i have a page in the local context and within this page i have got an iframe where the content of it is in the web context. Now I would like to send some data from the local context to the web context. How do i do this.

local

var node = document.createElement("iframe");
node.src = 'ms-appx-web:///pages/mapView/mapView.html?latitude=' + coordinates.latitude + '&longitude=' + coordinates.longitude;
node.style.width = '100%';
node.style.height = '100%';
node.onload = function () {
    node.contentWindow.postMessage(JSON.stringify(data), "ms-wwa-web://" + 
                                   document.location.host);
}
document.querySelector('#insert').appendChild(node);

in the iframe i do:

window.addEventListener('message', receiveMsg, false);

I have got an ajax call where i got some data, but because i have no access to ajax calls in web context i would like to send some data from this here to mapView.html.

The problem is. I would send some values via Get Parameter, but I have too much data for that.

any help?

Safari
  • 3,302
  • 9
  • 45
  • 64
  • 1
    perhaps HTML5's postMessage - see http://stackoverflow.com/questions/12827495/how-to-give-javascript-alert-from-web-contextiframe-in-metro-app/12832809#12832809 – Jim O'Neil Feb 08 '13 at 15:17
  • @JimO'Neil That's how I've done this in the past. – GotDibbs Feb 08 '13 at 15:37
  • ok, but in the example you send data from the web context to the local context. i want to do this the other way round. maybe important to mention is that i add the iframe dynamically – Safari Feb 08 '13 at 15:44
  • @safari So are you having error's with the code as you have it above? – GotDibbs Feb 08 '13 at 17:13
  • no i get no error. I just get no response in the receiveMsg function. It never reaches it. – Safari Feb 11 '13 at 09:15
  • 1
    @Safari Could you try switching the second parameter of your postMessage call to "*" and see if that works? – GotDibbs Feb 11 '13 at 15:12
  • @Safari Reformatted that as an answer for you and posted. You may be able to get it to work with a specific targetOrigin, its just a matter of figuring out what that origin actually is and apparently it is not what you have in your question :) – GotDibbs Feb 12 '13 at 13:56

1 Answers1

0

Try switching this line:

node.contentWindow.postMessage(JSON.stringify(data), "ms-wwa-web://" + 
                               document.location.host);

... to this:

node.contentWindow.postMessage(JSON.stringify(data), "*");

If that works, you should be ok doing that here in a Win 8 app (wouldn't do that in a web app for security reasons), but if you want to lock it down more I think you'd just need to change the second parameter to be one of:

  • "ms-www-web:///" (note the triple slash)
  • "ms-www-web:///pages/"
GotDibbs
  • 3,068
  • 1
  • 23
  • 29