Over a million developers have joined DZone.
Platinum Partner

Use WinJS to post a message from an iFrame to your Win8 App

· Web Dev Zone

The Web Dev Zone is brought to you in partnership with Mendix. Download this Forrester Report to gain a better understanding of the low-code platform market and how to make a strategic platform selection you won’t regret.

Sometimes, you display information in an iframe and you need to communicate it back to your Windows 8 app. You may think like I did “just navigate to a specific page with the arguments in the URL and make the Win8 app read the location of the iframe”…. Cool but all you’ll get is an “access denied”.

To have this kind of scenario, you have to do some more code on the server page …


The solution is maybe well known by the web developpers : use the postMessage function.

In the page displayed by the Windows8 App in an iframe, just call the postMessage on the window to send a message to the app. Don’t forget to set the origin to “*”:

window.parent.postMessage("this is a messsage very important","*");

In the windows 8 app, you can register to this kind of message with a very classic code. Also,you can check the origin field to be sure that the message comes from your web site.

window.addEventListener("message", messageReceived, false);
 
function receiveMessage(e) {
    if (e.origin === "http://jonathanantoine.com") {
 
    }
};

The Web Dev Zone is brought to you in partnership with Mendix. Better understand the aPaaS landscape and how the right platform can accelerate your software delivery cadence and capacity with the Gartner 2015 Magic Quadrant for Enterprise Application Platform as a Service.

Topics:

Published at DZone with permission of Jon Antoine , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}