Solution 1 :

You can use localstorage to persist the count.

Example when the page starts:

var cat = localStorage.getItem('miGato');

Example to save the count:

localStorage.setItem('miGato', 'Juan');

The documentation is:


Solution 2 :

Yes you can use Window.localStorage,

var cat = localStorage.setItem('miGato', 'Juan');

Problem :


I wanted to create a simple website containing a button. If I press the button 5 times, it says 5. When I refresh the page, it should still say 5.
And when You load the page, it also says 5 and you can go on clicking.

I know how to build a counter for ONE session and one user, but I have nooo idea how to implement it to be stored “online”.

I know that I need to implement a database to store the value and load the latest click-value once the website is opened but I dont know where to simply start


Comment posted by help section

Welcome to SO. You might find reading the site

Comment posted by GetSet

You could use “cookies”, “localstorage”, or a back-end file that stores the current count. You could even use a “get url” but why bother. In other words there are many ways to do what you want to do. What have you tried and if any, show your code?

Comment posted by Tim Bordasch

@GetSet This is my first experience with a database I dont really know where to start -> I created a website ( for a small Online Game I am playing. I also managed to create a small API to load some data from the players. But now I want to include a little game (count clicks and never reset) on my page to get a simple start with a database -> because it only needs to store one variable (the button click count) I think I cant really send you any code because (regarding databases) I`ve got none

Comment posted by Tim Bordasch

@GetSet > I opened a MYSQL Database on my provider (Ionos). Now I am figuring out how to store a variable / update it and load it on the page

Comment posted by GetSet

Going with a database is not necessary for your current project. A db will increase the complexity of your learning experience. But if you want to go that route, of course do so. As an alternative you can use the local storage by Alejandro Gonzalez below. This will circumvent the need to know who the user is via a db setup, since the “local storage” is on the user’s device. A “cookie” solution will also be comparable.