how do I make the click counter not erase

0

I have a click counter on a button, but I want the number of clicks to be fixed, not to be deleted when a new visitor enters, if not, to continue showing the amount that is has done, without using mysql or php

code:

<button id="view">click</button> 
window.onload =  function(){
var contador = 0;
document.getElementById("view").onclick = function(){
contador++;
alert(contador);
}
}
    
asked by Header90 05.07.2018 в 03:24
source

1 answer

0

If you do not have compatibility issues, a simple solution is to use the WebStorage API and localStorage . Something like this:

window.onload =  function(){
   // Revisar si ya hay algun dato guardado en la memoria. Si no, iniciamos con cero
   var contador = localStorage.getItem('contado') || 0; 
   document.getElementById("view").onclick = function(){
      contador++;
      // Guardamos el valor de contado en la memoria
      localStorage.contador = contador;
      alert(contador);
   }
}
    
answered by 05.07.2018 в 08:10