0

I have a folder called https://example.com/foldera and https://example.com/folderb. I want different local storage for these two. I am using simplecart.js. In this the code referring to local storage is

localStorage            = window.localStorage,

// storage
                save: function () {
                    simpleCart.trigger('beforeSave');

                    var items = {};
                    


                    // save all the items
                    simpleCart.each(function (item) {
                        items[item.id()] = simpleCart.extend(item.fields(), item.options());
                    });

                    localStorage.setItem(namespace + "_items", JSON.stringify(items));

                    simpleCart.trigger('afterSave');
                },

                load: function () {

                    // empty without the update
                    sc_items = {};

                    var items = localStorage.getItem(namespace + "_items");

                    if (!items) {
                        return;
                    }






/************ HTML5 Local Storage Support *************/
(function () {if (!this.localStorage)if (this.globalStorage)try {this.localStorage=this.globalStorage}catch(e) {}else{var a=document.createElement("div");a.style.display="none";document.getElementsByTagName("head")[0].appendChild(a);if (a.addBehavior) {a.addBehavior("#default#userdata");var d=this.localStorage={length:0,setItem:function (b,d) {a.load("localStorage");b=c(b);a.getAttribute(b)||this.length++;a.setAttribute(b,d);a.save("localStorage")},getItem:function (b) {a.load("localStorage");b=c(b);return a.getAttribute(b)},
removeItem:function (b) {a.load("localStorage");b=c(b);a.removeAttribute(b);a.save("localStorage");this.length=0},clear:function () {a.load("localStorage");for (var b=0;attr=a.XMLDocument.documentElement.attributes[b++];)a.removeAttribute(attr.name);a.save("localStorage");this.length=0},key:function (b) {a.load("localStorage");return a.XMLDocument.documentElement.attributes[b]}},c=function (a) {return a.replace(/[^-._0-9A-Za-z\xb7\xc0-\xd6\xd8-\xf6\xf8-\u037d\u37f-\u1fff\u200c-\u200d\u203f\u2040\u2070-\u218f]/g,
"-")};a.load("localStorage");d.length=a.XMLDocument.documentElement.attributes.length}}})();

I changed some lines like ,

var url = new URL(window.location.href);
var prefix = url.replace("https://example.com/", "");
simpleCart.each(function (item) {
                        items[prefix+item.id()] = simpleCart.extend(item.fields(), item.options());
                    });
...
...

It add prefix for localstorage, but it dosn't prevent from sharing space in localstorage as in foldera it gets foldera prefix and folderb it gets folderb prefix. But the item still present in both folders, only prefix changes.

  • You can't. The browsers use one local storage per domain. You could use different domains `https://foldera.example.com/` and `https://folderb.example.com/` – Thomas Sablik Feb 21 '21 at 13:13
  • @ThomasSablik [This](https://stackoverflow.com/questions/4201239), and [this](https://stackoverflow.com/questions/21946922) topic says something about location. Is this applicable in this topic. – Justine Chacko Feb 21 '21 at 13:22
  • There is one local storage per domain: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage I can't find a contradiction to this in your links. _"A key based on the location"_ is your prefix. – Thomas Sablik Feb 21 '21 at 13:25

2 Answers2

0

You can't. There is one local storage per domain.

Thomas Sablik
  • 15,040
  • 7
  • 26
  • 51
0

First answer is you can't as described by others that There is one local storage per domain but there is an alternative solution...

A year ago i was also facing same problem so i create this function you can use this. but as i am a new developer to JS and can't understand your code so this code may be diffrent from your requirment.

function my_local_storage(name,value,folder){
    if(folder=='' || folder==null){
        folder = window.location.pathname.slice(1);
    }
    if(typeof value!=='undefined' && value!==null && typeof(Storage)){
        if(value===''){
            localStorage.removeItem(folder.name);return true;
        }
        if(typeof value=='object'){
            value=JSON.stringify(value);
        }
        localStorage.setItem(folder.name,value);
        return true;
    }
    else if (typeof(Storage) && localStorage.getItem(folder.name) !== null)
    {
        return localStorage.getItem(folder.name);
    }
    return null;
}

To save new storage for key name and value John just call this function my_local_storage("name","John")

To get value of name key my_local_storage("name")

To remove value of name key my_local_storage("name","")

To Access other folders storage value use my_local_storage("name",null,"folder2")

Th. H Chauhan
  • 109
  • 1
  • 10