Cache JS Variables

PHOTO EMBED

Mon Nov 21 2022 15:45:58 GMT+0000 (Coordinated Universal Time)

Saved by @FabHol #php #array #javascript

//Use localStorage for that. It's persistent over sessions.

//Writing :
localStorage['myKey'] = 'somestring'; // only strings

//Reading :
var myVar = localStorage['myKey'] || 'defaultValue';

//If you need to store complex structures, you might serialize them in JSON. For example :

//Reading :
var stored = localStorage['myKey'];
if (stored) myVar = JSON.parse(stored);
else myVar = {a:'test', b: [1, 2, 3]};

//Writing :
localStorage['myKey'] = JSON.stringify(myVar);

//Note that you may use more than one key. They'll all be retrieved by all pages on the same domain.

//Unless you want to be compatible with IE7, you have no reason to use the obsolete and small cookies.
content_copyCOPY

Yet to be tested

https://stackoverflow.com/questions/14266730/js-how-to-cache-a-variable