25.02.2013 Views

Peter Lubbers - Pro HTML 5 Programming

Pro HTML 5 Programming

Pro HTML 5 Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

var defaultTitle = "Portal [http://portal.example.com:9999]";<br />

var notificationTimer = null;<br />

var targetOrigin = "http://chat.example.net:9999";<br />

function messageHandler(e) {<br />

if (e.origin == targetOrigin) {<br />

notify(e.data);<br />

} else {<br />

// ignore messages from other origins<br />

}<br />

}<br />

CHAPTER 5 ■ USING THE COMMUNICATION APIS<br />

function sendString(s) {<br />

document.getElementById("widget").contentWindow.postMessage(s, targetOrigin);<br />

}<br />

function notify(message) {<br />

stopBlinking();<br />

blinkTitle(message, defaultTitle);<br />

}<br />

function stopBlinking() {<br />

if (notificationTimer !== null) {<br />

clearTimeout(notificationTimer);<br />

}<br />

document.title = defaultTitle;<br />

}<br />

function blinkTitle(m1, m2) {<br />

document.title = m1;<br />

notificationTimer = setTimeout(blinkTitle, 1000, m2, m1)<br />

}<br />

function sendStatus() {<br />

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

sendString(statusText);<br />

}<br />

function loadDemo() {<br />

document.getElementById("sendButton").addEventListener("click", sendStatus, true);<br />

document.getElementById("stopButton").addEventListener("click", stopBlinking, true);<br />

sendStatus();<br />

}<br />

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

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

<br />

123

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

Saved successfully!

Ooh no, something went wrong!