[javascript]cookie操作测试

  |   9 |   Code |   javascript WWW

测试页面
尝试设置cookies里加入test cookie

源码

<script>
document.writeln("<br>")
function setCookie (cookieName, cookieValue, cookiePath, cookieExpires)
{
   cookieValue = escape(cookieValue);
   if (cookieExpires == "")
   {
      var nowDate = new Date();
      nowDate.setMonth(nowDate.getMonth() + 6);
      cookieExpires = nowDate.toGMTString();
   }
   if (cookiePath != "")
   {
      cookiePath = ";Path=" + cookiePath;
   }
   document.cookie = cookieName + "=" + cookieValue + 
      ";expires=" + cookieExpires + cookiePath;
}
function getCookieValue(cookieName)
{
   var cookieValue = document.cookie;
   var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");

  if (cookieStartsAt == -1)
  {
      cookieStartsAt = cookieValue.indexOf(cookieName + "=");
  }

   if (cookieStartsAt == -1)
   {
      cookieValue = null;
   }
   else
   {

      cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
      var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
      if (cookieEndsAt == -1)
      {
         cookieEndsAt = cookieValue.length;
      }
      cookieValue = unescape(cookieValue.substring(cookieStartsAt, cookieEndsAt));

   }

   return cookieValue;
}
setCookie("TestCookie","Yes","","");
if(getCookieValue("TestCookie")==null)
document.writeln("SetCookie wrong<br>");
else document.writeln("SetCookie sucess :<br>"+document.cookie+"<br>");
</script>
Comments
Write a Comment