[SalesForce] Local Web Storage for Apex

Hi I have written a plain HTML with javascript that gets an object from Local Storage and works just fine, when I moved the code to Visualforce page, I get NULL back

 console.log("inside Retrieve from LocalStorage");
    var retrivedValue = 'None';
    var retrivedValue = localStorage.getItem('LocalStorageKey', retrivedValue);
    $("#divDataLocalStorage").text(retrivedValue);
    console.log("inside end of retrieve from localstorge");
    console.log("retrieved value = ", retrivedValue);
     var storedTasks = localStorage.getItem('LocalStorageKey');
    var storedTasks = localStorage.getItem('LocalStorageKey');
    var ObjectData = JSON.parse(storedTasks);
     console.log('retrievedObject 2: ', ObjectData);
     $.each( ObjectData, function( key, value ) {
            console.log( key + ": " + value.speed );
            console.log(key + ": " + value.info );
    });

I don't get any console error , it just that Visualforce doesn't have access to my Local Web storage

I wonder If the local Storage is set outside of Salesforce by another local regular HTML Page , is it accessible with Salesforce platform? (They both get run by chrome)

Best Answer

Local Storage is (sub-)domain specific, as a security feature. Each domain gets its own storage area, and that storage area is limited to 5 MB of space. If you set an item "outside of salesforce", it'll be stored in that domain, not within Salesforce, Visualforce, etc. If you need to communicate between your custom HTML page and Visualforce, consider using Window.postMessage. Both windows must be open at the same time for this to work.

Related Topic