Peter Lubbers - Pro HTML 5 Programming
Pro HTML 5 Programming
Pro HTML 5 Programming
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
228<br />
CHAPTER 9 ■ USING THE <strong>HTML</strong>5 WEB STORAGE API<br />
}<br />
// update the row text with the new values<br />
var currentDistance = distance(incomingLat, incomingLong, finishLat, finishLong);<br />
// store the incoming user name and distance in storage<br />
window.sessionStorage[incomingId] = currentDistance;<br />
// display the new user data in the page<br />
displayRacerLocation(incomingId, currentDistance);<br />
This function takes a string in the format described previously, a semicolon-separated message<br />
containing the name, latitude, and longitude of a racer. Our first step is to split it into its component<br />
parts using the JavaScript split() routine to produce the incomingId, incomingLat, and incomingLong,<br />
respectively.<br />
Next, it passes the racer’s latitude and longitude, as well as the latitude and longitude of the finish<br />
line, to the distance utility method we defined earlier, storing the resulting distance in the<br />
currentDistance variable.<br />
Now that we actually have some data worth storing, we can look at the call which exercises <strong>HTML</strong>5<br />
Web Storage.<br />
// store the incoming user name and distance in storage<br />
window.sessionStorage[incomingId] = currentDistance;<br />
In this line, we use the sessionStorage object on the window to store the current distance of the<br />
racer from the finish line as a value under the name and ID of the racer. In other words, we will set a<br />
value on the session storage with the key being the racer’s name and the value being that racer’s<br />
distance from the finish. As you will see momentarily, this data will be retrieved from storage as the user<br />
navigates from page to page on the web site. At the end of the function, we call the displayLocation()<br />
routine we previously defined to make sure that this most recent location update is displayed visually in<br />
the current page.<br />
Now, on to our final function in our storage example—the load routine shown in Listing 9-8 that<br />
fires whenever visitors access the web page.<br />
Listing 9-8. Initial Page Load Routine<br />
// when the page loads, make a socket connection to the race broadcast server<br />
function loadDemo() {<br />
// make sure the browser supports sessionStorage<br />
if (typeof(window.sessionStorage) === "undefined") {<br />
document.getElementById("leaderboardStatus").inner<strong>HTML</strong> = "Your browser does��<br />
not support <strong>HTML</strong>5 Web Storage";<br />
return;<br />
}<br />
var storage = window.sessionStorage;<br />
// for each key in the storage database, display a new racer<br />
// location in the page<br />
for (var i=0; i < storage.length; i++) {<br />
var currRacer = storage.key(i);<br />
displayRacerLocation(currRacer, storage[currRacer]);<br />
}