Ashish Anand
Ashish Anand

Reputation: 3581

Retain Variable's Value after refreshing a webpage

I want to refresh a web page and retain the value of the variable (created in javascript) when the page is reloaded.

for eg: var cnt = 5; cnt++;

I want the value of 'cnt' to be 6 even after I refresh a page. How can I do it?

Upvotes: 0

Views: 1822

Answers (3)

delannoyk
delannoyk

Reputation: 1224

I don't see how you can do this, except by using cookies or session.

Upvotes: 1

zsitro
zsitro

Reputation: 1892

Or put them after the hash tag.

var cnt=5; -> refresh page with adding hash to the url:

http://domain.com/index.php#cnt=5

then extract it after reload...

Google: window.location.hash

Upvotes: 0

Abhidev
Abhidev

Reputation: 7253

Store the values in cookies or use browser local storage.

Read this for more info http://www.w3schools.com/html5/html5_webstorage.asp

Upvotes: 3

Related Questions