ack__
ack__

Reputation: 923

How can I store data between web pages?

How can I store some information between pages with JS ?

My webapp is using sql queries to populate a couple pages and those queries usually return the same result. Is there a way to store this result in a JS variable, and force a refresh with sql queries only at some events ?

And how efficient is this vs. SQL queries ? vs. a cookie ?

Upvotes: 2

Views: 1185

Answers (2)

Eran Medan
Eran Medan

Reputation: 45715

You can also follow the single page web app pattern and use Ajax, look up backbone.js for more info

If you go further, you can lookup frameworks such as Meteor that can be setup to push updates to the client only if there are changes (there are other ways, but I like how meteor does it)

Upvotes: 0

Robert Harvey
Robert Harvey

Reputation: 180777

You could try Local Storage.

function supportsLocalStorage() {
  try {
    return 'localStorage' in window && window['localStorage'] !== null;
  } catch (e) {
    return false;
  }
}

function saveGameState() {
    if (!supportsLocalStorage()) { return false; }
    localStorage["halma.game.in.progress"] = gGameInProgress;
    for (var i = 0; i < kNumPieces; i++) {
    localStorage["halma.piece." + i + ".row"] = gPieces[i].row;
    localStorage["halma.piece." + i + ".column"] = gPieces[i].column;
    }
    localStorage["halma.selectedpiece"] = gSelectedPieceIndex;
    localStorage["halma.selectedpiecehasmoved"] = gSelectedPieceHasMoved;
    localStorage["halma.movecount"] = gMoveCount;
    return true;
}

Upvotes: 5

Related Questions