Skip to content Skip to sidebar Skip to footer

Is Local Storage Persistant?

I'm using Local Storage for a Chrome extensions. When I set the local storage using localStorage['tracking'] = false; ... and then check to is local storage set using: if(localSto

Solution 1:

Try:

localStorage.setItem('tracking', false);

and

localStorage.getItem('tracking');

These are the methods used in the W3C API Specification.

Since the usage you mentioned is not specified, depending on browser implementation it is possible that you are adding new properties to the localStorage global variable in the current window object, which become unavailable when navigating away from the page.

Solution 2:

LocalStorage works only with strings. Check this explanation.

Solution 3:

I used to have the same problem. I would like to share my solution. However, my case may not be the same as your case.

My case ist that I had this code (localStrage['xxxx']='xxx';) in a content script. Then I had the problem when open the new tab or open new windows.

So, my solution is that I did not run this code by the content script directly. The content script will call 'chrome.extension.sendRequest' and the background.html will do set/read localStrage and return to the content script.

This will be the code in content script

chrome.extension.sendRequest({action: 'load'}, function (response) {
            if(response){
                  alert('set');
            } else {
                  alert('Not Set');
                  chrome.extension.sendRequest({action: 'set',value:'true'}, function (response) {});
            }
}

This will be the code in background.html.

chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
    if (request.action == 'load') {
        sendResponse(localStorage['tracking']);

            }elseif (request.action == 'set') {
                localStorage['tracking']=request.value;
            }
     });

Post a Comment for "Is Local Storage Persistant?"