How do I bind a function to the HTML5 localStorage change event using jQuery?
$(function () {
$(window).bind('storage', function (e) {
alert('storage changed');
});
localStorage.setItem('a', 'test');
});
I’ve tried the above but the alert is not showing.
Update: It works in Firefox 3.6 but it doesn’t work in Chrome 8 or IE 8 so the question should be more ‘How to bind to localStorage change event using jQuery for all browsers?’
It turns out that this is actually working correctly and I have misinterpreted the specification
In other words, a storage event is fired on every window/tab except for the one that updated the
localStorageobject and caused the event.So the event was not fired because I only had one window/tab open. If I place the above code in a page and open the page in two tabs I would see the event fired in the second tab.
This answer on the problem contains more details.