After a complaint from one of our users and running some tests it appears Firefox 15 and 16 (and probably older version) make it so if you disable cookies you also disable localStorage. You can't even create a polyfill for it as whenever you try to access window.localStorage
you get Error: The operation is insecure.
Throwing a try catch will let you check to see if it's disabled but won't let you replace the variable with your own solution. The following quick polyfill will not work because FF ignores setting the variable and will throw the same error when trying to access it:
try{
window.localStorage;
}catch(err){
window.localStorage = {
getItem: function(k){
return this.k;
},
setItem: function(k,v){
this.k = v;
}
};
}
The only solution seems to be move the "fake" localStorage to another variable but this would be annoying as we have lots of code and a js lib that rely on accessing this variable. Any solutions?
Edit: It is not optimal to just pop up an alert to tell the users that cookies are required. If visitors just want to view the site and not signup then they truly don't need cookies. But being a backbone.js application and passing around a lot of data, we do store stuff in localStorage quite a bit.
If you disable the cookie , then local storage will not work.
The two have different purposes, and hence different strengths and weaknesses. Cookies are intended to be read by the server, whereas localStorage can only be read by the browser. Thus, cookies are restricted to small data volumes, while localStorage can store more data.
Yes. HTML5 sessionStorage (and localStorage) can be disabled, and the data can also be cleared.
The following are limitations, and also ways to NOT use localStorage : Do not store sensitive user information in localStorage. It is not a substitute for a server based database as information is only stored on the browser. localStorage is limited to 5MB across all major browsers.
Since localStorage
is a reserved variable, you have no other sane option than using a wrapper with a different name. Wrap your own code and the real localStorage under this variable and replace localStorage
with the name of your wrapper. Annoying, but less annoying than losing changes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With