2

Using JavaScript, is it possible to save all variables on a page to local storage, and then re-load the stored variables when the page is refreshed or reloaded? I'm trying to save the variables to local storage and load them when the page is refreshed. This is what I've tried so far:

http://jsfiddle.net/jZVWk/1/

function saveAllVariables(){
    //save all variables on the page to local storage
}

function loadAllVariables(){
    //load all variables on the page from local storage, and re-create them using their original names
}

loadAllVariables(); //load all variables that were previously stored

if(typeof theName == "undefined"){
    var theName = prompt("Please enter your name:","Your name");
}

if(typeof currentTime == "undefined"){
    var currentTime = new Date();
}

document.body.innerHTML += "Time last visited: " + currentTime + "<br />";
document.body.innerHTML += "Your name : " + theName + "<br />";
var currentTime = new Date();
Anderson Green
  • 30,230
  • 67
  • 195
  • 328
  • 1
    Check this out: http://stackoverflow.com/questions/2226007/fetching-all-javascript-global-variables-in-a-page – Stegrex Mar 18 '13 at 18:50
  • `for (prop in window) { console.log(prop); }` Now you have all the variables. Not good for cross-browser compatibility though. – Vinay Mar 18 '13 at 18:50
  • @rid I want to save the state of the entire page, including all JavaScript objects. There are already a few questions on Stack Overflow about saving the state of a web page, but the only discuss saving the state of the DOM, and none of them discuss saving JavaScript variables. – Anderson Green Mar 18 '13 at 18:51
  • 1
    @Stegrex That looks useful, but I'll need some way to re-create the variables after they've been saved to local storage, when the page is reloaded. Basically, I'll need a way to obtain the name and value of each variable so that the variables can be re-created easily. – Anderson Green Mar 18 '13 at 18:54

1 Answers1

1

Sort of. If the variables you care about are all global, and don't depend on any non global data, you can check out this question: Fetching all (javascript) global variables in a page (Thanks Stegrex)

But that's not the whole story. In JS lots of data is persisted in hidden scopes. This has 2 problems:

  1. The objects may not be accessible from the global scope.
  2. Functions may depend on data in the scope in which they were created, but that isn't accessible from the global scope.

For instance:

var globalThing = 'global';
var makeCounter = function() {
  var count = 0;
  return {
    increment: function() { count++; },
    getCount:  function() { return count; }
  }
}
var counter = makeCounter();
counter.increment();
alert(counter.getCount());

The state of this code is now impossible to literally save and reconstitute. count is in a closure, hidden and inaccessible from the global scope. Without a more intelligent way to inspect and save the internal state of your objects, you can't preserve this structure.


So perhaps this isn't the approach you want to take. I'd bet good money there is a far cleaner way to do what you want. So the question becomes: why do you need this? And what are you trying to do?

I'd strongly suggest that you explicitly save just the data you need, and do not try to brute force save the entire universe.

In your case, that would be simply:

function saveOnlyImportantVaiables() {
  localStorage.theName = theName;
  localStorage.currentTime = currentTime;
}
Community
  • 1
  • 1
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • I suppose it would be better to just save the variables that are absolutely needed. So how could I just save the variables that I needed to local storage when the page was unloaded? – Anderson Green Mar 18 '13 at 19:05
  • 2
    Simply write: `localStorage.someKey = someValue` and then read: `var someValue = localStorage.someKey` – Alex Wayne Mar 18 '13 at 19:06
  • 3
    @AlexWayne At least recommend using the standard methods - `getItem` and `setItem` – Ian Mar 18 '13 at 19:20