HTML and CSS Reference
In-Depth Information
function CheckOnline() {
$.ajax({
type: "POST",
url: 'Clock.aspx/IsOnline',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
if (result.d == true) {
$("#status").html("You are Online!");
setTimeout(CheckOnline, 5000);
}
},
error: function () {
$("#status").html("You are Ofline!");
setTimeout(CheckOnline, 5000);
}
});
This code shows a function—
CheckOnline()
—that makes an
$.ajax()
request to a web method
named
IsOnline()
. The
IsOnline()
web method returns a Boolean flag. If the
$.ajax()
method is able to
invoke
IsOnline()
successfully, it indicates that a network connection is available. The
success
method
then sets the content of a
<div>
to “You are Online!” If there is no network connectivity, the call to
IsOnline()
fails, and the
error
function is called. The
error
function sets the content of the
<div>
to “You
are Offline!” You need to check for the online status periodically; hence the
CheckOnline()
function is
called every 5,000 milliseconds using the
setTimeout()
function.
The
IsOnline()
web method used by the
$.ajax()
call is shown in Listing 8-9.
Listing 8-9.
IsOnline()
Web Method
[WebMethod]
public static bool IsOnline()
{
return true;
}
The
IsOnline()
method returns
true
to indicate that a network connection to the server is available.
Updating an Offline Application
Once a cache manifest file is served to the browser, the browser begins using the application files from the
cache. But you may need to change the web application after it has been cached. There are two
possibilities in such cases:
• You can add iles to or remove them from the web application and make changes to
the cache manifest file.
• You can change one or more of the application iles (their content or code) but not
change the cache manifest file.
When you begin using an offline application, normally the browser checks with the server to see
whether a cache manifest file is available that's newer than the one that has been cached. If so, the browser
downloads the new cache manifest file and downloads the files as per that cache manifest. This browser