Supporting offline Web application development is another focus of HTML5. The so-called offline web applications are applications that can still run even when the device cannot access the Internet.
Developing offline web applications requires several steps. The first is to make sure the app knows whether the device has Internet access so it can take the correct action next. Then, the application must also have access to certain resources (images, Javascript, CSS, etc.) in order to work properly. Best, there must be a local space for users to save data, and reading and writing will not be hindered regardless of whether they have access to the Internet.
HTML5 and its related APIs make developing offline applications a reality.
Offline detectionTo know whether the device is online or offline, HTML5 defines a navigator.onLine attribute. A value of true indicates that the device can access the Internet, and a value of false indicates that the device is offline.
if (navigator.onLine) { // Work normally} else { // Perform tasks in offline state}
Since navigator.onLine has certain compatibility issues, in addition to the navigator.onLine attribute, in order to better determine whether the network is available, HTML5 also defines two events, online and offline.
These two events are triggered on the window object when the network switches between offline and online:
window.addEventListener('online', function() { // Works normally}); window.addEventListener('offline', function() { // Performs tasks when offline});
In practical applications, it is best to obtain the initial state through navigator.onLine after the page is loaded. Then use the above two events to determine whether the network connection status changes. When the above event is triggered, the value of the navigator.onLine property will also change, but this property must be manually polled to detect changes in network status.
application cacheHTML5's application cache, or appcache for short, is specifically designed for developing offline web applications. Appcache is a cache area separated from the browser's cache. To save data in this cache, use a manifest file that lists the resources to be downloaded and cached. Description file example:
CACHE MANIFEST# Commentfile.jsfile.css
Then quote in html:
<html manifest=./xxx.manifest>
The MIME type of the xxx.manifest file must be text/cache-manifest.
The core of this API is the applicationCache object. This object has a status attribute. The value of the attribute is a constant, indicating the following current status of the application cache:
Related events:
Generally speaking, these events will be triggered in the order mentioned above as the page loads. The above events can also be triggered manually by calling the update() method.
data storage CookiesHTTP Cookies, usually called cookies directly, are used on the client to store session information. The standard requires the server to send the Set-Cookie HTTP header as part of the response to any HTTP request, which contains session information. Server response header example:
HTTP/1.1 200 OK
Content-type: text/html
Set-Cookie: name=value
Other-header: other-header-value
The browser then Set-Cookies the session information, and then adds the Cookie HTTP header to each request to send the information back to the server, as shown below:
GET /index.html HTTP/1.1
Cookie: name=value
Other-header: other-header-value
The additional information sent back to the server can be used to uniquely verify which request the client came from.
Complete cookies include: