Alex Rivera | Logout

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

Asked 2012-10-09T17:44:01.960
10

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.

Edit
Report

1 Answer

1

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.

answered 2013-06-22T18:40:50.213

Your Answer