本文实例讲述了javascript设置和获取cookie的方法。分享给大家供大家参考,具体如下:
1. 设置cookie
function setcookie(cookiename,cookievalue,cookieexpires,cookiepath){  cookievalue = escape(cookievalue);//编码latin-1  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;}
2. 获取cookie
function getcookievalue(cookiename){  var cookievalue = document.cookie;  var cookiestartat = cookievalue.indexof(+cookiename+=);  if(cookiestartat==-1)  {    cookiestartat = cookievalue.indexof(cookiename+=);  }  if(cookiestartat==-1)  {    cookievalue = null;  }  else  {    cookiestartat = cookievalue.indexof(=,cookiestartat)+1;    cookieendat = cookievalue.indexof(;,cookiestartat);    if(cookieendat==-1)    {      cookieendat = cookievalue.length;    }    cookievalue = unescape(cookievalue.substring(cookiestartat,cookieendat));//解码latin-1  }  return cookievalue;}
例子:
cookie用户名:
密 码:记住用户名
注意:
由于google chrome浏览器为了安全只支持online-cookie,所以在本地测试时是没有效果的,需要上传到服务器试一下。
更多关于javascript操作cookie相关内容可查看本站专题:《javascript 操作 cookie相关知识汇总》及《jquery的cookie操作技巧总结》
希望本文所述对大家javascript程序设计有所帮助。
   
 
   