js对cookie的几个操作函数(仅作备忘)
js对cookie的几个操作函数,网上找的(仅作备忘)
function getCookieVal(offset) {
var endstr = document.cookie.indexOf(";", offset);
if (endstr == - 1) {
endstr = document.cookie.length;
}
return unescape(document.cookie.substring(offset, endstr));
}
function getCookie(name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
var j = 0;
while (i < clen) {
j = i + alen;
if (document.cookie.substring(i, j) == arg) return getCookieVal(j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function deleteCookie(name) {
var exp = new Date();
var cval = getCookie(name);
exp.setTime(exp.getTime() - 1);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
var gCookieExpDays = 80;
function setCookie(name, value) {
var argv = setCookie.arguments;
var argc = setCookie.arguments.length;
var exp = (argc > 2) ? argv[2] : gCookieExpDays;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
var expires = new Date();
deleteCookie(name);
expires.setTime(expires.getTime() + (exp * 24 * 60 * 60 * 1000));
document.cookie = name + "=" + value + "; expires=" + expires.toGMTString() + ((domain == null) ? "": ("; domain=" + domain)) + ((path == null) ? "": ("; path=" + path)) + ((secure == true) ? "; secure": "");
}
建议继续学习:
- cookie窃取和session劫持 (阅读:13077)
- curl 命令使用cookie (阅读:8823)
- 前端开发中Cookie那些事儿 (阅读:6292)
- 如何设置一个永远无法删除的Cookie (阅读:5372)
- 在浏览器中加密Cookie (阅读:4759)
- 网站统计:第一方Cookie和第三方Cookie (阅读:4176)
- Cookie安全漫谈 (阅读:3674)
- 一段Javascript的代码 (阅读:3385)
- 使用Http-only Cookie来防止XSS攻击 (阅读:3209)
- FlashCookie (阅读:2960)
扫一扫订阅我的微信号:IT技术博客大学习
- 作者:Dante 来源: Vimer
- 标签: cookie
- 发布时间:2010-03-24 22:28:36
-
[85] memory prefetch浅析
-
[56] 基本排序算法的PHP实现
-
[50] 深入浅出cassandra 4 数据一致性问
-
[43] 转载:cassandra读写性能原理分析
-
[40] javascript插入样式
-
[39] MySQL半同步存在的问题
-
[38] Inline Form Labels
-
[37] JS中如何判断字符串类型的数字
-
[36] 字符引用和空白字符
-
[34] 获取Dom元素的X/Y坐标