js读写cookie

<div id="link"></div>
<script>
function SetCookie(cookieName, cookieValue, path, domain, secure){
 var expires = new Date();
 expires.setTime(expires.getTime() + 9000000000);
 document.cookie = escape(cookieName) + ‘=‘ + escape(cookieValue)
 + (expires ? ‘; expires=‘ + expires.toGMTString() : ‘‘)
 + (path ? ‘; path=‘ + path : ‘/‘)
 + (domain ? ‘; domain=‘ + domain : ‘‘)
 + (secure ? ‘; secure‘ : ‘‘);
}
function GetCookie(name){ 
 var cookie_start = document.cookie.indexOf(name);
 var cookie_end = document.cookie.indexOf(";", cookie_start);
 return cookie_start == -1 ? ‘‘ : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
} 
function DelCookie(cookieName, cookieValue, path, domain, secure){ 
 var cookieValue="hello";
 var expires = new Date();
 expires.setTime(expires.getTime() - 100000);
 document.cookie = escape(cookieName) + ‘=‘ + escape(cookieValue)
 + (expires ? ‘; expires=‘ + expires.toGMTString() : ‘‘)
 + (path ? ‘; path=‘ + path : ‘/‘)
 + (domain ? ‘; domain=‘ + domain : ‘‘)
 + (secure ? ‘; secure‘ : ‘‘);
} 

//创建,或是修改cookie

SetCookie("name","123");
SetCookie("site","fsdf");

//读取cookie

GetCookie("name");

//作为超级链接
document.getElementById("link").innerHTML="http://xxx.com/?name="+GetCookie("name");
</script>
提问者评价

参考至: http://zhidao.baidu.com/link?url=fRS5Pa05JGP2LgKwVkcLtYyvQxRoSx9iq6LsyUbFkdlSIXR8VN3yHMCbyKO7CNSvYQEFDxpfszbm_n9G4XlRh_

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。