zoukankan      html  css  js  c++  java
  • Javascript操作Cookie的脚本 — CookieHelper

    var HttpCookie = function(name, value, expires, path, domain) {

    if (name)

    this.Name = name;

    if (value)

    this.Value = value;

    if (expires)

    this.Expires = expires;

    if (path)

    this.Path = path;

    if (domain)

    this.Domain = domain;

    };

    HttpCookie.prototype = {

    Name : '',

    Value : '',

    Expires : '',

    Path : '/',

    Domain : '',

    toCookie : function() {

    var NewCookie = this.Name + '=' + this.Value;

    if (this.Expires)

    NewCookie += (';expires=' + this.Expires);

    if (this.Path)

    NewCookie += (';path=' + this.Path);

    if (this.Domain)

    NewCookie += (';domain=' + this.Domain);

    return NewCookie;

    }

    }

    var CookieHelper = function() {

    };

    CookieHelper.ConvertToUTCString = function(hourNumber) {

    if (!hourNumber || hourNumber == 0)

    return null;

    var Timestamp = (new Date().getTime() + (hourNumber * 1000 * 60 * 60));

    return new Date(Timestamp).toUTCString();

    };

    CookieHelper.Set = function(cookieName, cookieValue, expireHour, path, domain) {

    var HC = new HttpCookie(cookieName, escape(cookieValue), CookieHelper

    .ConvertToUTCString(expireHour), path, domain);

    document.cookie = HC.toCookie();

    };

    CookieHelper.Get = function(cookieName) {

    var regex = new RegExp(("(^| )" + cookieName + "=([^;]*)(;|$)"));

    var Matchs = document.cookie.match(regex);

    if (Matchs)

    return (Matchs[2]);

    return null;

    };

    CookieHelper.Delete = function(cookieName, path, domain) {

    if (!CookieHelper.Get(cookieName))

    return;

    var HC = new HttpCookie(cookieName, null, CookieHelper

    .ConvertToUTCString(-100));

    document.cookie = HC.toCookie();

    };

    使用示例:

    添加COOKIE,设置COOKIE的值:

    CookieHelper.Set(cookieName, cookieValue, expireHour, path, domain);

    示例:

    CookieHelper.Set('cookie_name', 'cookie_value', 1);

    //删除COOKIE CookieHelper.Delete('cookie_name');

    //获取COOKIE的值 CookieHelper.Get('cookie_name');

    原文出处:http://www.zu14.cn/2010/08/16/javascript-cookie-helper/

  • 相关阅读:
    Pandas包对多个数据表(DataFrame)的常用整合功能。
    pandas numpy 简单应用 loandata
    榛果 美团 登录 爬虫 requests session
    python 日期循环
    opencv 验证码 识别
    运行MapReduce任务
    CenOS安装MySQL服务
    leetcode 67. 二进制求和
    最近对一些领域比较感兴趣,这里列举出来供以后查阅
    leet code 1014. 最佳观光组合
  • 原文地址:https://www.cnblogs.com/suizhikuo/p/4962011.html
Copyright © 2011-2022 走看看