1

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)

1
  • Did you save the "LocalStorageKey" after moving it to the Apex Page? The storage domain could be different when done from apex vs when you stored the Key originally
    – Eric
    Commented Nov 10, 2016 at 19:55

1 Answer 1

4

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.

7
  • The final goal is to have both pages built in Apex. do you think once I have the other page that Saves the LocalStorageKey inside salesforce Ill have access to the saved information? Also If I render the page as PDF does the Local Storage and the rest of the Javascript stuff runs before the page render to PDF?
    – Metawaa
    Commented Nov 10, 2016 at 21:09
  • @Metawaa As far as your first question, yes, all Visualforce pages in the same namespace on the same server share the same localStorage (each namespace gets its own localStorage, though). I've used techniques like this before successfully. As for the second question, JavaScript, localStorage, etc do NOT work in PDF mode. The PDF is generated on the server, not your browser, so there's no way to have any scripts run. You'll have to pass that data as a parameter or another technique to get the data into the PDF.
    – sfdcfox
    Commented Nov 10, 2016 at 21:20
  • So I cant render the page as PDF with Javascript content in it? but JavaScript is the only way I can get all the information out on the page
    – Metawaa
    Commented Nov 10, 2016 at 21:29
  • @Metawaa Sorry, you'll have to come up with an alternative method. JavaScript can't run in PDF, because basically the server renders a static page and then converts it into a PDF format. You can save the data into a record somewhere, and use that, or if it's a small amount of data, use a page parameter.
    – sfdcfox
    Commented Nov 10, 2016 at 21:33
  • is there anyway to send the HTML after rendering to salesforce and ask it to create a new PDF page? use POST or get
    – Metawaa
    Commented Nov 10, 2016 at 22:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .