Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
109
rated 0 times [  114] [ 5]  / answers: 1 / hits: 39583  / 12 Years ago, sat, april 21, 2012, 12:00:00

I can successfully bind an event for a change to localStorage (using jquery):



$(window).bind('storage', function(e)
{
alert('change');
});

localStorage.setItem('someItem', 'someValue');


If I use sessionStorage, the event will NOT fire:



 $(window).bind('storage', function(e)
{
alert('change');
});

sessionStorage.setItem('someItem', 'someValue');


Why is this?


More From » jquery

 Answers
13

That is the way it's meant to be I think. From the spec (emphasis added):




When the setItem(), removeItem(), and clear() methods are called on a
Storage object x that is associated with a session storage area, if
the methods did something, then in every Document object whose Window
object's sessionStorage attribute's Storage object is associated with
the same storage area, other than x, a storage event must be fired




I think what that means is that the event will be fired in any other document sharing the session storage object, but not the document that caused the event to fire.



Update



Here's another very similar question which seems to agree with what I've mentioned above (the answer quotes the same paragraph from the spec).


[#86092] Friday, April 20, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tyemathewj

Total Points: 484
Total Questions: 107
Total Answers: 111

Location: Equatorial Guinea
Member since Sun, Feb 14, 2021
3 Years ago
;