...
For example, a Web site can display a count of how many times the user has visited a page with the following script.
Code Block | ||
---|---|---|
| ||
<p> You have viewed this page <span id="count">an untold number of</span> time(s). </p> <script> var storage = window.localStorage; if (!storage.pageLoadCount) storage.pageLoadCount = 0; storage.pageLoadCount = parseInt(storage.pageLoadCount, 10) + 1; document.getElementById('count').innerHTML = storage.pageLoadCount; </script> |
Note: Before incrementing pageLoadCount it must first be converted to a number with the parseInt Method (JScript 5.6).
Each domain and subdomain has its own separate local storage area. Domains can access the storage areas of subdomains, and subdomains can access the storage areas of parent domains. For example, localStorage['example.com']
is accessible to example.com
and any of its subdomains. The subdomain localStorage['www.example.com']
is accessible to example.com
, but not to other subdomains, such as mail.example.com
.
The Storage Object
The following properties and methods are supported by both session and local storage objects.
Topic | Contents |
---|---|
clear | Removes all key/value pairs from the Web Storage area. |
constructor | Returns a reference to the constructor of an object. |
getItem | Retrieves the current value associated with the Web Storage key. |
key | Retrieves the key at the specified index in the collection. |
length | Retrieves the length of the key/value list. |
remainingSpace | Retrieves the remaining memory space, in bytes, for the storage object. |
removeItem | Deletes a key/value pair from the Web Storage collection. |
setItem | Sets a key/value pair. |
Web Storage Events