cookie有这么几个属性,name,value,expireDate,domain,path
项目遇到html/person/person.html设置的cookie无法从html/alipay/pay.asp中读出
结果:cookie的路径问题,person.html默认的path是自己的路径,想要让隔壁路径的网页访问该cookie须设置path=/
之后,一并把domain也弄清楚了,www.test.com/test/test.aspx默认domain为www.test.com,ti.test.com设置的cookie要让t2.test.com访问须设置domain=.test.com
js原生封装函数cookie:
1 var CookieUtil={ 2 get:function(name){ 3 var cookieName=encodeURIComponent(name)+"=", 4 cookieStart=document.cookie.indexOf(cookieName), 5 cookieValue=null; 6 if(cookieStart>-1){ 7 var cookieEnd=document.cookie.indexOf(";",cookieStart); 8 if(cookieEnd==-1){ 9 cookieEnd=document.cookie.length; 10 } 11 cookieValue=decodeURIComponent(document.cookie.substring(cookieStart+cookieName.length,cookieEnd)); 12 } 13 return cookieValue; 14 }, 15 set:function(name,value,expires,path,domain,secure){ 16 car cookieText=encodeURIComponent(name)+"="+ 17 encodeURIComponent(value); 18 if(expires instanceof Date){ 19 cookieText+=";expires="+expires.toGMTString(); 20 } 21 if(path){ 22 cookieText+=";path="+path; 23 } 24 if(domain){ 25 cookieText+=";domain="+domain; 26 } 27 if(secure){ 28 cookieText+=";secure"; 29 } 30 document.cookie=cookieText; 31 }, 32 unset:function(name,path,domain,secure){ 33 this.set(name,"",new Date(0),path,domain,secure); 34 } 35 };
时间: 2024-10-29 19:09:57