JavaScript 对cookie 的增、删、获取

jopen 13年前
     <pre class="brush:javascript; toolbar: true; auto-links: false;">/* * 设置cookie 名称,值,过期时间(0 为删除cookie),域名,路径,安全 ,过期时间以天为单位 */ function setCookie(name,value,exp,path,domain,secure) {  var expDays = exp*24*60*60*1000; var expDate = new Date(); expDate.setTime(expDate.getTime()+expDays); var expString = ((exp == null) ? “” : (“;expires=”+expDate.toGMTString())) var pathString = ((path == null) ? “/” : (“;path=”+path)) var domainString = ((domain == null) ? “” : (“;domain=”+domain)) var secureString = ((secure == true) ? “;secure” : “” )  if(domain == ” || typeof(domain)==’undefined’) domain = ‘818.cn’;  return document.cookie = name + “=” + escape(value) + expString + pathString + domainString + secureString;  }</pre>    <pre class="brush:javascript; toolbar: true; auto-links: false;">/* * 获得cookie id */ function getCookie(name) { var result = null; var myCookie = document.cookie + “;”; var searchName = name + “=”; var startOfCookie = myCookie.indexOf(searchName); var endOfCookie; if (startOfCookie != -1) { startOfCookie += searchName.length; endOfCookie = myCookie.indexOf(“;”,startOfCookie); result = unescape(myCookie.substring(startOfCookie, endOfCookie)); }  return result; }</pre>    <p>注意:当设置cookie 到时候有域参数,那么在删除的时候也要给定域,否则删不掉的,这个问题闹了一个小时。</p>