You actually don't need jQuery for this at all. HTML5 itself has a neat facility for applications ran in online/offline conditions - cache manifest. The cache manifest is a plain text file referenced in the manifest
attribute of <html>
tag:
<html manifest="cache.manifest">
The contents of this manifest file tells the browser, which resources need online conditions to function (NETWORK
), which files to cache (CACHE
) and what to do when network resource is requested while offline (FALLBACK
).
CACHE MANIFEST
# the above line is required
NETWORK:
*
FALLBACK:
* error404.html
CACHE:
index.html
about.html
help.html
# Plus all other resources required to make it look nice
style/default.css
images/logo.png
images/backgound.png
When using manifest, browser first consults the manifest and, without any scripting, acts accordingly based on being online or offline.
http://dev.w3.org/html5/spec-preview/offline.html#manifests
UPDATE:
Please note the crucial requirement on response content type. Plain text simply doesn't work.
Manifests must be served using the text/cache-manifest
MIME type.
http://dev.w3.org/html5/spec-preview/offline.html#writing-cache-manifests
Also be aware that all URLs not listed in the manifest are blocked from load as a result. That's why I've fixed the above manifest with wildcards (*
) to allow "the rest". That might help you get started.