0

我想在 cookie 中设置输入值,并希望在点击功能中发出警报。我也想知道 cookie 的到期日期。我研究了 cookie 功能,但它给出了空白值。小提琴

function setCookie(c_name,value,extime)
{
var exdate=new Date();
exdate.setTime(exdate.getTime() + extime);
var c_value=escape(value) + ((extime==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
4

1 回答 1

1

试试这个:

设置方法:

function setCookie(name, value, days)
{
  var expires;
  if (days)
  {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    expires = "; expires=" + date.toGMTString();
  }

  else
  {
    expires = "";
  }

  document.cookie = name + "=" + value + expires + "; path=/";
};

获取方法:

function getCookie(name)
{
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for (var i = 0; i < ca.length; i++)
  {
    var c = ca[i];
    while (c.charAt(0) == ' ') c = c.substring(1, c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
  }
  return null;
};

我已经分别更改了 jsfiddle:http: //jsfiddle.net/DVeVm/

于 2013-10-14T12:54:29.783 回答