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.

}<br />

if (!window.localStorage) {<br />

log("<strong>HTML</strong>5 Local Storage not supported in your browser.");<br />

return;<br />

}<br />

if (!window.WebSocket) {<br />

log("<strong>HTML</strong>5 WebSocket is not supported in your browser.");<br />

return;<br />

}<br />

CHAPTER 10 ■ CREATING <strong>HTML</strong>5 OFFLINE WEB APPLICATIONS<br />

log("Initial cache status: " + showCacheStatus(window.applicationCache.status));<br />

document.getElementById("installButton").onclick = install;<br />

Adding the Update Button Handler<br />

Next, add an update handler that updates the application cache as follows:<br />

install = function() {<br />

log("Checking for updates");<br />

try {<br />

window.applicationCache.update();<br />

} catch (e) {<br />

applicationCache.onerror();<br />

}<br />

}<br />

Clicking this button will explicitly start the cache check that will cause all cache resources to be<br />

downloaded if necessary. When available updates have completely downloaded, a message is logged in<br />

the UI. At this point, the user knows that the application has successfully installed and can be run in<br />

offline mode.<br />

Add Geolocation Tracking Code<br />

This code is based on the geolocation code from Chapter 4. It is contained in the tracker.js JavaScript<br />

file.<br />

/*<br />

* Track and report the current location<br />

*/<br />

var handlePositionUpdate = function(e) {<br />

var latitude = e.coords.latitude;<br />

var longitude = e.coords.longitude;<br />

log("Position update:", latitude, longitude);<br />

if(navigator.onLine) {<br />

uploadLocations(latitude, longitude);<br />

}<br />

storeLocation(latitude, longitude);<br />

}<br />

255

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

Saved successfully!

Ooh no, something went wrong!