// 创建cookie
function Cookie() { }
// //set_cookie 把设置cookie方法绑定在cookie的原型对象上
Cookie.prototype.setCookie = function (key, value, time) {
var date_ = new Date();
// 设置时间
date_.setDate(date_.getDate() + time)
// 页面输出
document.cookie = key + '=' + value + '; expires = ' + date_;
}
// //删除cookie
Cookie.prototype.removeCookie = function (key) {
this.setCookie(key, '', -1)
}
// 获取cookie中的值
Cookie.prototype.getCookie = function (key) {
// 把cookie中的值分割
var arry = document.cookie.split('; ')
for (var i; i < arry.length; i++) {
// 遍历数据 然后用=号进行分割
var arry_ = arry[i].split('=');
if (arry_[0] == key) {
return arry_[1];
}
}
}
// 删除所有的cookie
Cookie.prototype.clereCookie = function () {
// 分隔值
var arry = document.cookie.split('; ')
for (var itrm of arry) {
var arry_ = arry[itrm].split('=')
this.setCookie(arry_[0], '', -1);
console.log(arry_[0]);
}
}
使用原型对象封装cookie
最新推荐文章于 2024-09-04 20:18:34 发布