SoFunction
Updated on 2025-04-04

VUE front-end cookies are simple to operate

The following is a simple cookie operation. Currently only for front-end instances. The specific content is as follows

There are two things to note:

1. The name of the cookie content storage
2. Delete cookies by setting expired to past time

<body>
<div >
 <button @click="clearCookie()">
 Clearcookie
 </button>
</div>
</body>
<script>
 let app = new Vue({
 el: "#app",
 data: {
 },
 created: function () {
  ();
 },
 methods: {
  //Set cookies  setCookie: function (cname, cvalue, exdays) {
  var d = new Date();
  (() + (exdays * 24 * 60 * 60 * 1000));
  var expires = "expires=" + ();
  (cname + "=" + cvalue + "; " + expires);
   = cname + "=" + cvalue + "; " + expires;
  ();
  },
  //Get cookies  getCookie: function (cname) {
  var name = cname + "=";
  var ca = (';');
  for (var i = 0; i < ; i++) {
   var c = ca[i];
   while ((0) == ' ') c = (1);
   if ((name) != -1) return (, );
  }
  return "";
  },
  //Clear cookies  clearCookie: function () {
  ("username", "", -1);

  },
  checkCookie: function () {
  var user = ("username");
  if (user != "") {
   alert("Welcome again " + user);
  } else {
   user = prompt("Please enter your name:", "");
   if (user != "" && user != null) {
   ("username", user, 365);
   }
  }
  }
 }
 })
</script>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.