25.02.2013 Views

Peter Lubbers - Pro HTML 5 Programming

Pro HTML 5 Programming

Pro HTML 5 Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

124<br />

CHAPTER 5 ■ USING THE COMMUNICATION APIS<br />

Cross-Origin Portal<br />

Origin: http://portal.example.com:9999<br />

Status <br />

Change Status<br />

<br />

This uses postMessage to send a status update to the widget iframe contained in the portal<br />

page.<br />

<br />

<br />

<br />

Stop Blinking Title<br />

<br />

Listing 5-4 shows the code for the portal page postMessageWidget.html.<br />

Listing 5-4. Contents of postMessageWidget.html<br />

<br />

widget<br />

<br />

<br />

var targetOrigin = "http://portal.example.com:9999";<br />

function messageHandler(e) {<br />

if (e.origin === "http://portal.example.com:9999") {<br />

document.getElementById("status").textContent = e.data;<br />

} else {<br />

// ignore messages from other origins<br />

}<br />

}<br />

function sendString(s) {<br />

window.top.postMessage(s, targetOrigin);<br />

}<br />

function loadDemo() {<br />

document.getElementById("actionButton").addEventListener("click",<br />

function() {<br />

var messageText = document.getElementById("messageText").value;<br />

sendString(messageText);<br />

}, true);<br />

}<br />

window.addEventListener("load", loadDemo, true);<br />

window.addEventListener("message", messageHandler, true);<br />

<br />

Widget iframe<br />

Origin: http://chat.example.net:9999<br />

Status set to: by containing portal.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!