<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
  <META NAME="Generator" CONTENT="EditPlus">
  <META NAME="Author" CONTENT="">
  <META NAME="Keywords" CONTENT="">
  <META NAME="Description" CONTENT="">
 </HEAD> <BODY>
  <script>
function starta()
{
    var username="";
    if(document.cookie!="")
    {
        username=document.cookie.split(":")[0].split("=")[1];
    }
    document.getElementById("Text1").value=username;
    document.getElementById("Text1").onblur=setcookie;
}
function setcookie()
{
    var date=new Date();
   // date.setDate(date.getDate+1);
    var username=document.getElementById("Text1").value;
    document.cookie="username="+username+":expire="+date.toGMTString();
alert("username="+username+":expire="+date.toGMTString());
    //alert(username);
}  </script>
  <input type="text" id="Text1" value="" width=100>
  <input type="button" value="setCookie" onclick="setcookie()">
   <input type="button" value="start" onclick="starta()">
 </BODY>
</HTML>

解决方案 »

  1.   

    比较完整的,封装了的Js Cookie 例子:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script language="javascript" type="text/javascript">
    var JsCookie = new function()
    {
    //计算cookie有效期
    this.GetExpDate = function (days, hours, minutes)
    {
    var expDate = new Date();
    if(typeof(days) == "number" && typeof(hours) == "number" && typeof(hours) == "number")
    {
    expDate.setDate(expDate.getDate() + parseInt(days));
    expDate.setHours(expDate.getHours() + parseInt(hours));
    expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
    return expDate.toGMTString();
    }
    }

    //取得一个cookie的值
    this.GetCookie = function (name)
    {
    var ck   = document.cookie;
    var exp1 = new RegExp(name + "=.*?(?=;|$)");
    var mch  = ck.match(exp1);
    return mch? mch[0].substring(name.length+1) : null;
    }

    //增加或修改一个cookie
    this.SetCookie = function (name, value, expires, path, domain, secure)
    {
    document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
    }

    //删除一个cookie(使cookie过期)
    this.DeleteCookie = function (name,path,domain) 
    {
    if(this.GetCookie(name))
    {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
    }
    }//设置一个Cookie
    JsCookie.SetCookie("cookie_1","Cookie测试 - 值",JsCookie.GetExpDate(1));
    //读取这个Cookie
    alert("添加了Cookie之前:\n\n" + unescape(JsCookie.GetCookie("cookie_1")));
    //删除这个Cookie
    JsCookie.DeleteCookie("cookie_1");
    alert("删除之后:\n\n" + unescape(JsCookie.GetCookie("cookie_1")));
    </script>
    </head><body>
    </body>
    </html>