skip to Main Content

I’m making a website with html + css + python and I want to make a button where the user can click it but only once. Sort of like a like button on facebook. I don’t have accounts for my users, but I was thinking of using cookies instead (less robust I know, but my website will be simple enough where this is fine).

Do you guys know how I can achieve this?

I’ve done a bunch of googling and trial and error, but nothing seems to work. My counter either doesn’t increment, or it does but the value resets every time I reload the website.

2

Answers


  1. You can use localStorage mechanism:

    https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

    <button onclick='myOnClick()'>click here</button>
    <script>
    function myOnClick(){
      let myData = localStorage.getItem( 'myButtonStorage' );
      if ( myData == 'clicked' ) {
        alert ( 'already clicked' );
      } else {
        localStorage.setItem( 'myButtonStorage', 'clicked' );
        //
        // here do your action
        console.log ( 'do sth...' );
      }
    }
    </script>
    
    Login or Signup to reply.
  2. You can create a const an set to true if its active. And you verify each time you click

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search