<1>设置cookie
<script type="text/javascript">
window.onload=function(){
document.cookie="myName=C_world";
}
<script>
<2>获取cookie
<script type="text/javascript">
window.onload=function(){
var myName;
var btnGetCookie=document.getElementById("btnGetCookie");
btnGetCookie.onclick=function(){
var strCookie=document.cookie;
var eleCookie=strCookie.split(";");
for(var i=0;i<eleCookie.length;i++){
str nameCookie=eleCookie[i].split("=");
if("myName"==nameCookie[0]){
myName=nameCookie[1];
alert(myName);
}
}
}
}
<script>
<3>给cookie加有效时间。//含有两个参数的方法,保存用户名和保存时间。
<script type="text/javascript">
function SetCookieExpires(myName,expiresDays){
var date=new Date();
date.setTime(date.getTime()+expiresDays*24*3600*1000);
document.cookie="myName="+myName+";expires="+date.toGMTString();
}
</script>
<4>删除Cookie//设置myName的Cookie过期.即假删除。
<script type="text/javascrip">
function DeleteCookie(){
var date=new Date();
document.cookie="myName=Cworld; expires="+(date.getTime()-10000).toGMTString();
}
</script>