技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> JavaScript --> js对cookie的几个操作函数(仅作备忘)

js对cookie的几个操作函数(仅作备忘)

浏览:1612次  出处信息

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": "");
}

建议继续学习:

  1. cookie窃取和session劫持    (阅读:12893)
  2. curl 命令使用cookie    (阅读:8756)
  3. 前端开发中Cookie那些事儿    (阅读:6164)
  4. 如何设置一个永远无法删除的Cookie    (阅读:5220)
  5. 在浏览器中加密Cookie    (阅读:4567)
  6. 网站统计:第一方Cookie和第三方Cookie    (阅读:4105)
  7. Cookie安全漫谈    (阅读:3526)
  8. 一段Javascript的代码    (阅读:3318)
  9. 使用Http-only Cookie来防止XSS攻击    (阅读:3067)
  10. FlashCookie    (阅读:2887)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
  • 作者:Dante    来源: Vimer
  • 标签: cookie
  • 发布时间:2010-03-24 22:28:36
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1