The example demonstrates how to get the location when the user is in offline mode. In order to run the application, server support is needed.Start the server by issuing the following command in python.
[html]
Python –m SimpleHTTpServer 9999
[/html]
After starting the server user can redirect to the page
http://localhost :9999/tracking.html.
To build the tracking application user should have the manifest file with
.appcahe
extension the code is as shown below.
[html]CACHE MANIFEST
# JavaScript
./offline.js
#./tracker.js
./log.js
# stylesheets
./html5.css
# images
[/html]
Now build the
.html
file. For example, in
tracker.html the code is as shown below.
[html]<!DOCTYPE html>
<html lang="en" manifest="tracker.appcache">
<head>
<title> HTML5 Offline Application</title>
<script src="log.js"> </script>
<script src="offline.js"> </script>
<script src="tracker.js"> </script>
<link rel="stylesheet" href="html5.css">
</head>
<body>
<header>
<h1> Offline Example</h1>
</header>
<section>
<article>
<button id="installButton"> Check for Updates</button>
<h3> Log</h3>
<div id="info">
</div>
</article>
</section>
</body>
</html>
[/html]
Then create the
multiple.js files and the code below is used to build
offline.js file.
[html]/*
* log each of the events fired by window.applicationCache
*/
window.applicationCache.onchecking = function(e) {
log("Checking for application update");
}
window.applicationCache.onnoupdate = function(e) {
log("No application update found");
}
window.applicationCache.onupdateready = function(e) {
log("Application update ready");
}
window.applicationCache.onobsolete = function(e) {
log("Application obsolete");
}
window.applicationCache.ondownloading = function(e) {
log("Downloading application update");
}
window.applicationCache.oncached = function(e) {
log("Application cached");
}
window.applicationCache.onerror = function(e) {
log("Application cache error");
}
window.addEventListener("online", function(e) {
log("Online");
}, true);
window.addEventListener("offline", function(e) {
log("Offline");
}, true);
/*
* Convert applicationCache status codes into messages
*/
showCacheStatus = function(n) {
statusMessages = ["Uncached","Idle","Checking","Downloading","Update Ready","Obsolete"];
return statusMessages[n];
}
install = function() {
log("Checking for updates");
try {
window.applicationCache.update();
} catch (e) {
applicationCache.onerror();
}
}
onload = function(e) {
// Check for required browser features
if (!window.applicationCache) {
log("HTML5 Offline Applications are not supported in your browser.");
return;
}
if (!navigator.geolocation) {
log("HTML5 Geolocation is not supported in your browser.");
return;
}
if (!window.localStorage) {
log("HTML5 Local Storage not supported in your browser.");
return;
}
log("Initial cache status: " + showCacheStatus(window.applicationCache.status));
document.getElementById("installButton").onclick = checkFor;
}
var storeLocation = function(latitude, longitude) {
// load stored location list
var locations = JSON.parse(localStorage.locations || "[]");
// add location
locations.push({"latitude" : latitude, "longitude" : longitude});
// save new location list
localStorage.locations = JSON.stringify(locations);
}
[/html]
The code below is used to build
log.js file as follows.
[html]log = function() {
var p = document.createElement("p");
var message = Array.prototype.join.call(arguments, " ");
p.innerHTML = message;
document.getElementById("info").appendChild(p);
}
[/html]
The code below is used to build
Tracker.js file.
[html]/*
* Track and report the current location
*/
// When the page loads, set the status to online or offline
function loadDemo() {
if (navigator.onLine) {
log("Online");
} else {
log("Offline");
}
}
// Now add event listeners to notify a change in online status
window.addEventListener("online", function(e) {
log("Online");
}, true);
window.addEventListener("offline", function(e) {
log("Offline");
}, true);
var handlePositionUpdate = function(e) {
var latitude = e.coords.latitude;
var longitude = e.coords.longitude;
log("Position update:", latitude, longitude);
if(navigator.onLine) {
uploadLocations(latitude, longitude);
}
storeLocation(latitude, longitude);
}
var handlePositionError = function(e) {
log("Position error");
}
var uploadLocations = function(latitude, longitude) {
var request = new XMLHttpRequest();
request.open("POST", "http://geodata.example.net:8000/geoupload", true);
request.send(localStorage.locations);
}
var geolocationConfig = {"maximumAge":20000};
navigator.geolocation.watchPosition(handlePositionUpdate,
handlePositionError,
geolocationConfig);
[/html]
Now add the local storage in offline mode which is as shown below.
[html]var storeLocation = function(latitude, longitude) {
// load stored location list
var locations = JSON.parse(localStorage.locations || "[]");
// add location
locations.push({"latitude" : latitude, "longitude" : longitude});
// save new location list
localStorage.locations = JSON.stringify(locations);
}
[/html]
Output
By running the above code in a preferred browser, following output appears.