HTML and CSS Reference
In-Depth Information
JavaScript APIs have provided some new powerful functionality, such as the ability to store
more data locally and make that data available to the webpage through the Web Storage API.
The AppCache API allows you to take web applications offline. The Geolocation API provides
methods to work with global positioning within the application.
This objective covers how to:
■
Use the storage API
■
Use the AppCache API
■
Use the Geolocation API
WebA Storage is a new API for storing webpage data locally. Web Storage, described in this
section, replaces the concept of cookies.
NOTE
BROWSER SUPPORT
Of course, you must consider your audience's browser support for HTML5 and Web Storage
before you choose to use it exclusively.
Two forms of Web Storage exist:
local
and
session
storage. Local storage is persistent; data
stored in local storage is available to the webpage even if the user closes the browser com-
pletely and then reopens it to your site. Session storage is available only for the duration of
the current session, so if the user closes the browser, session storage is automatically cleaned
up and is no longer available. The Web Storage API is available as a global object. To access
local storage, use the
localStorage
object; to access session storage, use the
sessionStorage
object.
EXAM TIP
The
localStorage
and
sessionStorage
objects provide exactly the same API. All the examples
shown in this section work exactly the same with either object. The only difference is the
lifetime of the storage. Remember that
sessionStorage
is cleared when the session is closed,
whereas
localStorage
is still be accessible after a session closes and a new session opens.
Table 1-13 lists the API methods and their usage. Web Storage is implemented as name
value pairs and stored as strings. Any data that you can put into a string format can be stored
in Web Storage. This isn't as limiting as it sounds. You'll see some examples of storing complex
objects.
Search WWH ::
Custom Search