EDUDOTNET

Monday, March 14, 2016

How to get javascript variable to retain value after page refresh?

This is possible to using cookies, localStorage or sessionStorage etc.

We can use localStorage for it. we need to set a varibale that should be reflected in next pages or after reload the page.
 eg.:

var myval = "xyz";

localStorage.setItem("myval", myval);


after that we can get the stored value from localStorage by page reloaded/Refresh...

like:
var myval = localStorage.getItem("myval");


The complete list of alternatives of cookies is:


  • Standard HTTP cookies
  • Local Shared Objects (Flash cookies)
  • Silverlight Isolated Storage
  • Storing cookies in RGB values of auto-generated, force-cached PNGs using HTML5 Canvas ag to read pixels (cookies) back out
  • Storing cookies in Web history
  • Storing cookies in HTTP ETags
  • Storing cookies in Web cache
  • window.name caching
  • Internet Explorer userData storage
  • HTML5 Session Storage
  • HTML5 Local Storage
  • HTML5 Global Storage
  • HTML5 Database Storage via SQLite

0 comments:

Post a Comment