js实现cookie的存取值方式

/**
* cookie中存值
* */
function setCookie (name, value) {
if (value) {
var days = 1; //定义一天
var exp = new Date();
exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
// 写入Cookie, toGMTString将时间转换成字符串
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString;
}
};

/**
* cookie中取值
* */
function getCookie (name) {
var arr,reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); //匹配字段
if (arr = document.cookie.match(reg)) {
return unescape(arr[2]);
} else {
return null;
}
};

/**
* 清除指定cookie值
* */
function delCookie (name) {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = setCookie(name);
if (cval && cval != null) {
document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString()
}
};

/**
* 清除全部cookie值
* */

function clearCookie() {
var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
if (keys) {
for (var i = keys.length; i--;) {
// document.cookie = keys[i] +'=0;expires=' + new Date( 0).toUTCString()
document.cookie = keys[i] +'=0;expires=' + new Date( 0).toUTCString() + ";path=/video_learning" + ";domain=localhost";
}
}
};

注意:清除cookie的时候,需要同时删除path和 domain,否则会出现cookie没有被清除的情况出现。

详情链接:https://blog.csdn.net/weixin_38047955/article/details/78832643

 

/**
* cookie中存值
* */
function setCookie(name, value) {
if (value) {
var days = 1; //定义一天
var exp = new Date();
exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
// 写入Cookie, toGMTString将时间转换成字符串
// document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString;
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/video_learning" + ";domain=localhost";
}
};

注意:在cookie中存储时,一定要注意把path改为根路径,并加上domain;否则在其他页面是无法获取到存储的cookie值的。

 

/**
* cookie中存值(存放多长时间)
* */
function setCookie(name, value, expiredays) {
if (value) {
var days = 1; //定义一天
var exp = new Date();
exp.setTime(exp.getTime() + expiredays);
// 写入Cookie, toGMTString将时间转换成字符串
document.cookie = name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exp.toGMTString()) + ";path=/" + ";domain=localhost";
}
};

 

/**
* cookie中获取域名
* */
function GetCookieDomain() {
var host = location.hostname;
var ip = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
if (ip.test(host) === true || host === 'localhost') return host;
var regex = /([^]*).*/;
var match = host.match(regex);
if (typeof match !== "undefined" && null !== match) host = match[1];
if (typeof host !== "undefined" && null !== host) {
var strAry = host.split(".");
if (strAry.length > 1) {
host = strAry[strAry.length - 2] + "." + strAry[strAry.length - 1];
}
}
return '.' + host;
}

转载于:https://www.cnblogs.com/YanSmallKind/p/11274850.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值