Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

javascript - Simple cookie test

how do i display the tag "username" as "John Doe"
My website holding the html code is here, hello!

<p id="motherhome">
  Cookie test
</p>
<button onclick="creation()">
  Create
</button>
<button onclick="ragnarok()">
  Delete
</button>
<button onclick="showhw()">
  Display
</button>
<p id="pancreas"></p>
<script>
function creation(){
  document.cookie = "username=John Doe;"
}
function ragnarok(){
  document.cookie = "username=John Doe;";
}
function showhw(){
  document.getElementById("pancreas").innerHTML;
}
</script>
question from:https://stackoverflow.com/questions/66056435/simple-cookie-test

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I had to declare the cookie a variable to use it, the way i made it is inefficient but a quick solution.

 <p id="motherhome">
      Cookie test
    </p>
    <button onclick="creation()">
      Create
    </button>
    <button onclick="ragnarok()">
      Delete
    </button>
    <button onclick="showhw()">
      Display
    </button>
    <p id="pancreas">no cookie hahah</p>
    <script>
    function creation(){
      document.cookie = "username=John Doe; expires=Thu, 18 Dec 2021 12:00:00 UTC; path=/lostch1.html";
    }
    function ragnarok(){
      document.cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/lostch1.html";
    }
    function showhw(){
      document.getElementById("pancreas").innerHTML = username;
    }
      var username = document.cookie;
    </script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...