How to enter a cookie in this code

Asked

Viewed 128 times

0

Guys I have a code that makes a link turn green for 10 seconds and then turns blue, the only problem is that if I update the page within those 10 seconds it comes back all over again. So I wanted to know how to make the action continue where it left off even after updating, see the code:

<a id="id1" href="https://br.answers.yahoo.com" onclick="myFunction(event)" target="_blank">Clique Aqui</a> <br/><br/>

<script language="javascript">
    function myFunction(s) {
        var id = s.target.id;

        document.getElementById(id).style.color = "green";
        setTimeout(function(){
            document.getElementById(id).style.color = "blue";
        }, 10000);
    }
</script>
  • Good, you can always try using localStorage.

1 answer

1

If you want it to continue indefinitely until you delete it, you can do this with Html5, or if you want it to forget the data when closing the browser as well.

INDEFINITELY: // Store

localStorage.setItem("lastname", "Smith");

// Receive

var lastname = localStorage.getItem("lastname");

// Remove

localStorage.removeItem("lastname");

TEMPORARILY // Store

sessionStorage.setItem("lastname", "Smith");

// Receive

var lastname = sessionStorage.getItem("lastname");

// Remove

sessionStorage.removeItem("lastname");
  • How do I put in my code? I have to put another name in the place of lastname? (I’m layman yet)

  • The name you refer to, for example, I will refer to a name I would put localStorage.setItem("name", "Leonardo");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.