18

i am working with one project in which variable is assigned a value i need that value when browser is refreshed.Is there any way get that value?

Anudeep GI
  • 931
  • 3
  • 14
  • 45

4 Answers4

22

Cookies

The best solution is to use cookies. A cookie, also known as an HTTP cookie, web cookie, or browser cookie, is usually a small piece of data sent from a website and stored in a user's web browser while a user is browsing a website (from Wikipedia).

Using a cookie you can save the state and later read it and use it.

With jQuery it is very easy to use cookies.

To set:

$.cookie("var", "10");

To get:

$.cookie("var")

To delete:

$.cookie("var", null);

Local Storage

When you want to save localy great amount of data, you have another opportunity — to use local storage (since HTML5). You can do it directly using JavaScript or using one of available jQuery plugins.

for example, with totalStorage:

var scores = new Array();
scores.push({'name':'A', points:10});
scores.push({'name':'B', points:20});
scores.push({'name':'C', points:0});
$.totalStorage('scores', scores);
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
4

You can place that value in a cookie.

The complete list of alternatives 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

Evercookie

Hasan Fathi
  • 5,610
  • 4
  • 42
  • 60
Eric J.
  • 147,927
  • 63
  • 340
  • 553
3

You can store it in a cookie. Or use local storage if supported by the browser.

Jan Hančič
  • 53,269
  • 16
  • 95
  • 99
2

You can use $.cookie("key", value) to set the value, and $.cookie("key") to get the saved value.

EDIT: A small tutorial on how you use cookies in jQuery: http://www.electrictoolbox.com/jquery-cookies/

FredrikO
  • 1,982
  • 1
  • 12
  • 8