cookie值的设置,获取及删除

<script>

function setCookie( key, val, expire){
var dateTime = new Date();
dateTime.setTime( dateTime.getTime() + (expire)*24*3600*1000);
document.cookie = key + "="+ val + "; expires=" + dateTime.toGMTString();
}
function delCookie(key){
setCookie(key, "", -1 );
}
function getCookie( name ){
var cookies = document.cookie;
var cookieArr = cookies.split("; ");
for( var i = 0; i < cookieArr.length; i++ ){
var cookie = cookieArr[i].split("=");
if( cookie[0] == name ){
return cookie[1];
}
}
}
function set(){
setCookie("aaa","landen",10);
}
function del(){
delCookie("aaa");
}
function get(){
alert(getCookie( "aaa"))
}

</script>

<body>

  <input type="button" id="setBtn" onclick="set()" value="设置">

  <input type="button" id="delBtn" onclick="del()" value="删除">

  <input type="button" id="getBtn" onclick="get()" value="获取">

</body>

posted @ 2017-03-21 20:39  曦言鸢默  阅读(260)  评论(0编辑  收藏  举报