Get and Set localStorage in Salesforce Visualforce Page

Get and Set localStorage in Salesforce Visualforce Page

In Salesforce Visualforce Page, we can get and set localStorage using JavaScript localStorage.setItem() and logcalStorage.getItem() methods.

Sample Visualforce Page Code:

<apex:page>
    <div id="txt">        
    </div>
    <script>
        localStorage.setItem( 
            'exampleItem', 
            'Testing' 
        );
        console.log( 
            'exampleItem is', 
            localStorage.getItem( 'exampleItem' ) 
        );    
    	document.getElementById( "txt" ).innerHTML = localStorage.getItem( 'exampleItem' );
    </script>
</apex:page>

Output:

Leave a Reply