Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solution for Firefox user disabling cookies and thus localStorage - polyfill not possible

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.

like image 593
Mauvis Ledford Avatar asked Oct 09 '12 17:10

Mauvis Ledford


People also ask

Does disabling cookies disable local storage?

If you disable the cookie , then local storage will not work.

Can I use local storage instead of cookies?

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.

Can user Block localStorage?

Yes. HTML5 sessionStorage (and localStorage) can be disabled, and the data can also be cleared.

When should you not use localStorage?

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.


1 Answers

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.

like image 135
Ast Derek Avatar answered Oct 09 '22 16:10

Ast Derek