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?
4 Answers
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);

- 61,765
- 13
- 122
- 144
-
Using cookies is _not_ the best solution, because cookies are also shared with the web server. – Alnitak Sep 13 '13 at 19:48
-
@Alnitak It usually will be if you are required to support browsers which don't have local storage. – rogermushroom Sep 20 '13 at 20:58
-
https://github.com/carhartl/jquery-cookie – Anis Abboud Jun 10 '15 at 03:53
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

- 5,610
- 4
- 42
- 60

- 147,927
- 63
- 340
- 553
You can store it in a cookie. Or use local storage if supported by the browser.

- 53,269
- 16
- 95
- 99
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/

- 1,982
- 1
- 12
- 8