How to save your data counter when you reload the page (JS)?

Welcome!
There is a button which when pressed adds +1 to the counters. The goal is to make sure that each device the data is preserved when you reload the page. The code of the button:
<script type="text/javascript">
var clicks = 0;
function onClick() {
 clicks += 1;
 document.getElementById("clicks").innerHTML = clicks;
};
function onReset() {
 if (confirm('are You sure? The data will be reset!')){
 clicks = 0;
 document.getElementById("clicks").innerHTML = clicks;
}
}
</script>
March 23rd 20 at 19:15
3 answers
March 23rd 20 at 19:17
Solution
The final working solution looks like this. Suddenly someone come in handy.

<script type="text/javascript">
 var clicks = localStorage.getItem('clicks') || 0;
 document.addEventListener("DOMContentLoaded", function(){ document.getElementById("clicks").innerHTML = localStorage.getItem('clicks') || 0; });
 function onClick() {
 clicks = +clicks + 1;
 document.getElementById("clicks").innerHTML = clicks;
updateStorage(clicks);
};

 function onReset() {
 if (confirm('are You sure? The data will be reset!')) {
 clicks = 0;
 document.getElementById("clicks").innerHTML = clicks;
updateStorage(clicks);
}
}

 function updateStorage(clicks) {
 localStorage.setItem('clicks', clicks);
}
 </script>
March 23rd 20 at 19:19
March 23rd 20 at 19:21
On each device -- i.e. clicked in the browser, and the phone updated? Then the answer is the same: keep the value of the counter buck
No, without reference to the identity of the user. But with reference to browser. Generally it is counter to pedestrian traffic. A man comes to think of the people opens the a site and stupidly clicked on a button to remember, therefore there is a risk that he will turn it off or accidentally refreshes the page, and that in such cases, should not stray kaunter. It somehow solved Local Storage, but I can't - beverly_Legros commented on March 23rd 20 at 19:24
@beverly_Legros, well, in that case, the solution using cookies you came)
According to your update, try this:
var clicks = localStorage.getItem('clicks') || 0;
document.getElementById("clicks").innerHTML = clicks;
...
- Bryon.Beatty84 commented on March 23rd 20 at 19:27

Find more questions by tags Web DevelopmentHTMLJavaScript