25.02.2013 Views

Peter Lubbers - Pro HTML 5 Programming

Pro HTML 5 Programming

Pro HTML 5 Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 9 ■ USING THE <strong>HTML</strong>5 WEB STORAGE API<br />

• The removeItem(key) function does exactly as you might expect. If a value is<br />

currently in storage under the specified key, this call will remove it. If no item was<br />

stored under that key, no action is taken.<br />

■ Note Unlike some collection and data frameworks, removing an item does not return the old value as<br />

a result of the call to remove it. Make sure you’ve stored any copy you need independent of the removal.<br />

• Finally, the clear() function removes all values from the storage list. It is safe to<br />

call this on an empty storage object; as such, a call will simply do nothing.<br />

DISK SPACE QUOTA<br />

<strong>Peter</strong> says: “The specification recommends that browsers allow five megabytes per origin. Browsers<br />

should prompt the user when the quota is reached in order to grant more space and may also provide<br />

ways for users to see how much space each origin is using.<br />

In reality, the behavior is still a bit inconsistent. Some browsers silently allow a larger quota or prompt for a<br />

space increase, while others simply throw the QUOTA_EXCEEDED_ERR error shown in Figure 9-3. The test<br />

file testQuota.html used in this example is located in the code/storage directory.”<br />

Communicating Web Storage Updates<br />

Sometimes, things get a little more complicated, and storage needs to be accessed by more than one<br />

page, browser tab, or worker. Perhaps your application needs to trigger many operations in succession<br />

whenever a storage value is changed. For just these cases, the <strong>HTML</strong>5 Web Storage API includes an event<br />

mechanism to allow notifications of data updates to be communicated to interested listeners. Web<br />

Storage events are fired on the window object for every window of the same origin as the storage<br />

operation, regardless of whether or not the listening window is doing any storage operations itself.<br />

■ Note Web Storage events can be used to communicate between windows on the same origin. This will be<br />

explored a bit more thoroughly in the “Practical Extras” section.<br />

To register to receive the storage events of a window’s origin, simply register an event listener, for<br />

example:<br />

window.addEventListener("storage", displayStorageEvent, true);<br />

221

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!