HTML5 Guidelines for Web Developers

(coco) #1

216 Chapter 8—Web Storage and Offline Web Applications


The error event can be especially useful when trying to locate errors. A file listed
in the cache manifest that cannot be found fires this event in the browser. The
browser aborts any script execution from this point on, a situation in which you
probably would not think of first during debugging. More on debugging can be
found in section 8.2.3, Debugging.
The JavaScript API offers two additional methods for the cache: update() and
swapCache(). With these methods you can update the cache without reloading
the page, for example, via an Update button. The following HTML fragment cre-
ates the button:

<button onclick="window.applicationCache.update();">
update applicationCache</button>

We handle the updateready event in the JavaScript code:

window.applicationCache.addEventListener("updateready",
function(e) {
window.applicationCache.swapCache();
alert("New Cache in action");
}, false);

As soon as the update has been successfully downloaded, the function swap-
Cache() overwrites the old cache with the updated version. The update function
first checks the cache manifest file. If it has not changed, no update takes place
regardless of whether individual files for the cache have changed or not. The same
result as clicking the button with the mouse can be achieved by reloading the page.
There can be situations in which manual or automatic control of the cache can
be appropriate, for example, for a monitor without user interaction displaying
current news in a public space. The cache can be updated in the background via
a continuously repeating function (setInterval()). The system can then display
HTML pages reliably with or without network access.
The specification prescribes another attribute that indicates whether the brows-
er is online or offline. window.navigator.onLine is meant to return the value false
if the browser is set to not access the network or is sure that network access will
fail. In all other cases, the variable returns true.

Even if the value of window.navigator.onLine is true, this does not automatically
mean that the browser has access to the Internet. The browser can also be
online in private networks without necessarily being connected to the public
Internet.

NOTE
Free download pdf