JS implementation of the method to clear the specified cookies

  • 2020-03-30 03:57:20
  • OfStack

This article illustrates a practical way to clear a specified cookie with a JS implementation. Share with you for your reference.

The specific implementation code is as follows:


function GetCookieValue(name) {
  var cookieValue = null;
  if (document.cookie && document.cookie != '') {
 var cookies = document.cookie.split(';');
 for (var i = 0; i < cookies.length; i++) {
   var cookie = jQuery.trim(cookies[i]);
   //PYYH=USERNAME=steven&PASSWORD=111111&UserID=1&UserType=1
   if (cookie.substring(0, name.length + 1) == (name + '=')) {
 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
 //USERNAME=steven&PASSWORD=111111&UserID=1&UserType=1
 break;
   }
 }
  }
  return cookieValue;
}
function DelCookie(name) {
  var exp = new Date();
  exp.setTime(exp.getTime() + (-1 * 24 * 60 * 60 * 1000));
  var cval = GetCookieValue(name);
  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function exit() {
  DelCookie("PYYH");
  window.location.replace("http://localhost:7877/index.aspx");
}


Related articles: