task002_5-bom

设置cookie

1
2
3
4
5
function setCookie(cookieName, cookieValue, expiredays) {
var oDate = new Date();
oDate.setDate(oDate.getDate() + expiredays);
document.cookie = cookieName + '=' + cookieValue + ';expires=' + oDate;
}

获取cookie值

1
2
3
4
5
6
7
8
9
10
function getCookie(cookieName) {
var arr = document.cookie.split('; ');
for (var i = 0; i < arr.length; i++) {
var arr2 = arr[i].split('=');
if(arr2[0] === cookieName) {
return arr2[1];
}
}
return '';
}

删除cookie值

1
2
3
function removeCookie(cookieName) {
setCookie(cookieName, '1', -1);
}

应用cookie实现一个记住用户名的demo,同时在console打印了以上3种方法供测试(推荐用FF打开)
在线demo