0

Scenario:

Let's say we have and app which works like:

 Header

 Content

 footer

Header and Footer are statics and content is a div which is called via ajax to load an specific view.

So basically, our first view is login, after calling a login controlling, we get a list and move across the app w/o changing the link structure cause the ajax div content calling.

Problem:

When we refresh, we go back to the login page instead of the main view, which displays all the options which comes with the login controller answer. I'm trying to keep those options into a global variable, however after refreshing, all data is erased and there is not way I can go back to the main view even if session is alive (by session cookie), I only can back if I send the user and pw again, which I cannot. Eventually, storing the user and pw into cookies is not an option.

Any advise to solve this issue?

andresmijares
  • 3,658
  • 4
  • 34
  • 40
  • Seems like [web storage](http://www.jermywells.com/2014/04/javascript-web-storage-friend-or-foe.html) would work well for this. – J.Wells May 28 '14 at 18:20
  • You need to store the users login state somewhere. Either in local storage, session storage, cookies, or on the server. – mchandleraz May 28 '14 at 18:20
  • possible duplicate of [Global Variable usage on document.ready reload](http://stackoverflow.com/questions/29986657/global-variable-usage-on-document-ready-reload) – Stephan Muller Jun 05 '15 at 09:39

1 Answers1

1

You can use DOM Storage to store the value of your global variables between reloads.

DOM Storage is a client side key/value storage.

localStorage.setItem('key', value);
value = localStorage.getItem('key');

There are two variants:

  1. sessionStorage, this will store the value for the time of the users session
  2. localStorage, this will keep the data between sessions

If you have to support ancient browsers you can use cookies to store values between reloads.

src
  • 205
  • 1
  • 8
  • sorry for my ignorance, but when u say, "for the time of the users session", what exactly do you mean? as long as my session cookie is alive? – andresmijares May 28 '14 at 19:19
  • "A page session lasts for as long as the browser is open and survives over page reloads and restores." -- from the DOM Storage link – src May 28 '14 at 19:25