HTML and CSS Reference
In-Depth Information
online.txt offline.txt
These files can then be used to determine
the online status. In this example it's
linked to a button: each time the button is
clicked, the online status is checked and
The full listing is in the files ch06/offline-
example/offline-5.html and ch06/offline-
example/offline-checker.js. The key parts
of the code are shown next.
Here's the function that's called when the button is clicked. It calls
another function in the external JavaScript file, passing two functions
as parameters. The first function is executed if the server's available,
the second if the server's offline:
function display_online_status() {
function() { log('online'); },
function() { log('offline'); }
return false;
In a real application, the functions passed in would do something use-
ful, such as synchronize the application data with the server or queue it
for later delivery. But in this example, all they do is log the state of the
connection to the page.
Finally, here's the function that does all the real work. Most of this is
standard AJAX boilerplate; using any one of the popular JavaScript
libraries will reduce the function to about four lines of code. The key
Search WWH ::

Custom Search