Retain Variable's Value after refreshing a webpage
-
08-12-2019 - |
Question
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?
Solution
I don't see how you can do this, except by using cookies or session.
OTHER TIPS
Store the values in cookies or use browser local storage.
Read this for more info http://www.w3schools.com/html5/html5_webstorage.asp
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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow