javascript - Why does Internet Explorer fire the window "storage" event on the window that stored the data? -


internet explorer 10 fires window "storage" event on same window stored local storage.

it seems other browsers fire event on other windows, don't have worry window that's listening storage events reacting own storing. why ie fire event in wrong window, , how can replicate standard behavior in ie?

microsoft seems aware of issue, doesn't they're going fix time soon: https://connect.microsoft.com/ie/feedback/details/774798/localstorage-event-fired-in-source-window

one option can design listeners , setters don't react or store when information storage event that's consistent state. however, design pattern can more difficult relying on storage event firing on other windows.

another option make local storage work same way in ie works in other browsers. came admittedly hacky functional solution tested in ie10 , expect work in ie8 , ie9. javascript components don't listen window "storage" event directly instead listen 'event dispatcher' created. event dispatcher listens window "storage" event , triggers backbone event unless storage event originated in window. way check if window stored value using custom method calling setitem() creates unique id value , keeps track of it, this:

eventdispatcher.set = function(key, value) {   var storageid = (new date()).gettime();   eventdispatcher.idlist.push(storageid);   localstorage.setitem(key, {value: value, id: storageid}); } 

and eventdispatcher listens storage events , triggers event if list of ids created doesn't contain value's id, this:

$(window).on('storage', dispatchstorageevent); function dispatchstorageevent(event) {   var newvalue = json.parse(event.originalevent.newvalue);   if (eventdispatcher.idlist.indexof(newvalue['id']) > -1) {     return;   }   eventdispatcher.trigger("storageevent:" + event.originalevent.key, newvalue['value']); } 

there's bit more code can add keep idlist short , create empty array in first place, left out sake of clarity. making eventdispatcher handle storage events bit of overhead, can make development consistent between browsers. less hacky solution use proper locking , queueing mechanism, can difficult.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -