568 CHAPTER 15 Local data with web storage
alert(e.originalEvent.newValue);
}
$(window).on('storage', respondToChange);
if ('onstorage' in document) // bind to document for IE8
$(document).on('storage', respondToChange);
Using events with sessionStorage
In the previous lesson, you learned that browser context dictates when data can be shared.
Because the context for localStorage includes other tabs and windows, notifications are
passed to each open instance. However, sessionStorage gains little benefit from events
because its context includes the active tab only. Current browsers have included <iframe>
elements within that context definition, so it is possible to pass notifications to and from them
if necessary.
Lesson summary
■■Other tabs and windows can subscribe to storage events to receive notifications when
a change occurs to localStorage.
■■The StorageEvent object passed to subscribers contains detailed information regarding
what changes were made.
■■Because sessionStorage data is not shared beyond the current tab or window, others
will not receive notifications when a change occurs.
■■Storage events cannot be canceled and do not bubble up.
Lesson review
Answer the following questions to test your knowledge of the information in this lesson. You
can find the answers to these questions and explanations of why each answer choice is correct
or incorrect in the “Answers” section at the end of this chapter.
- Which of the following is not a property of the StorageEvent object?
A. oldValue
B. key
C. changeType
D. storageArea - If you modify a value stored in sessionStorage, which of the following could receive
notifications of the change (if subscribed)?
A. Another tab opened to a page on the same domain
B. A second browser window open to the same page