AJAX - The Complete Reference

(avery) #1

PART III


Chapter 10: Web Services and Beyond 547


We can also remove items using navigator.offlineResources.remove(), passing it
the URL string of what we want to remove from the offline store:

navigator.offlineResources.remove("http://ajaxref.com/ch10/offlineimage.gif");

For bulk removal use the clear() method:

navigator.offlineResources.clear(); // no more storage

As a list of resources, we can look at the length of the offlineResources:

alert(navigator.offlineResources.length); // How many items

We can also look at particular items numerically:

alert(navigator.offlineResources.item(1)); // What’s at position 1

And we can query the list to see if a particular URL is in the list:

if (navigator.offlineResources.has("http://ajaxref.com/ch10/secretplans.html"))
alert("The plans are safely saved offline!");

NNOT EOTE The process of saving files for offline use may take some time, and it is possible the user will
go offline before it is done. There are interfaces to address this possibility, but at the time of this
edition’s writing they are still somewhat in flux. Check Firefox’s documentation for the latest
information on navigator.pendingOfflineLoads and the load events associated with it.

An example similar to the Gears offline storage demo but using Firefox 3’s native offline
support is shown here:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Chapter 10 : Firefox 3 Offline Browsing</title>
</head>
<script type="text/javascript">
var prefix = "http://ajaxref.com/ch10/";
var filesToStore = [ "offlinestorage.html" , "offlinepage.html" ,
"images/rufus.jpg" , "scripts/alert.js" ];
function createStore()
{
var i;
for (var i=0; i < filesToStore.length; i++)
{
try {
navigator.offlineResources.add(prefix+filesToStore[i]);
} catch (e) { };
}
}
function removeStore()
{
navigator.offlineResources.clear();
Free download pdf