How can I explain the behavior of events in different repositories between browsers?

Error implementing localStorage in Chrome? it explains the specification very well and how it behaves differently in Opera and IE9.

But what now?

This will really mess up my code (any code) that relies on implementation, one way in the browser, when it's different.

How can I check if an event is fired in a window that triggers it or not?

The only thing I can understand is to use setTimeout () to wait and see if the event is fired at the caller or not, and then do something if it is not. But, this can make some terrible mistakes when a storage event is called quickly.

In my case, I need to fire an event for all windows, but not all, except for those who call it. In Chrome, which correctly implements it in the specification, it is just a matter of adding an extra function after adding it to localStorage.setItem (), but then in IE, Firefox3.6 and Opera it will do it twice effectively. I could also have different browser-based code, but I have to test for compatibility, not the browser version.

Is there a way to "schedule" all browsers with localStorage support so that all events are handled equally at all?

+4
source share
2 answers

I solved my own problem a bit

var localStorageSpecCompliant = true; var specCompliantTestVal = new Date().getTime(); function onStorage(ev) { if( ev.key == "specCompliantTest"+specCompliantTestVal ){ localStorageSpecCompliant = false; } localStorage.removeItem("specCompliantTest"+specCompliantTestVal); }; if( window.addEventListener ){ window.addEventListener("storage", onStorage, false); } else { window.attachEvent("onstorage", onStorage); } localStorage["specCompliantTest"+specCompliantTestVal] = "This tests if the browsers localStorage storage event is spec compliant or ont."; localStorage_setItem = function(key, value){ localStorage[key] = value; if( localStorageSpecCompliant ){ var dirtyFakeEvent = {"key":key, "newValue":value}; onStorage(dirtyFakeEvent); } } 

But some patch that always makes the event work the same will be much better. This path is not perfect. I could not figure out how to fake a real "storage" event, so I immediately called the function.

A frame that simply corrects compliance would be awesome. I have no problem with localStorage not existing in IE7. But functionality that is completely different in one browser or another that is different from the specification is so problematic.

+1
source

I was curious about this and looked at some js libraries. The one I have found so far that supports the storage event is the YUI 2 Storage Utility. It would be interesting to take a look and see if it normalizes behavior in browsers. There is also a YUI3-based library that looks interesting and advertises a change event and a storage ready event.

YUI 2 storage

YUI 3 Based Storage Lite

They may contain more dependencies than you want. They may also offer ways to learn how to solve a problem more simply.

These libraries also solve the question of what to do with browsers that do not offer HTML5-style storage.

0
source

Source: https://habr.com/ru/post/1412192/


All Articles