zoukankan      html  css  js  c++  java
  • JS 操作 cookie

    export function cookie(cookieName, cookieValue, day) {
      function readCookie (name: string) {
        const reg = new RegExp(`(^| )${name}=([^;]*)(;|$)`);
        const matched = document.cookie.match(reg);
        const arr = matched;
        if (arr) {
          return unescape(arr[2]);
        }
          return null;
      }
      function setCookie(name, value, time) {
        const Days = time || 30;
        const exp = new Date();
        exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
        document.cookie = `${name}=${escape(value)};expires=${exp.toUTCString()}`;
      }
      if (cookieName && cookieValue) {
        // set cookie
        setCookie(cookieName, cookieValue, day);
        return cookieValue;
      } if (cookieName && cookieValue === null) {
        // delete cookie
        setCookie(cookieName, '', -1);
        return cookieValue;
      } if (cookieName) {
        // read cookie
        return readCookie(cookieName);
      }
      return null;
    }
    

    ts 版本

    export function cookie(cookieName:string, cookieValue?:string, day?:number) {
      function readCookie (name: string) {
        const reg = new RegExp(`(^| )${name}=([^;]*)(;|$)`);
        const matched = document.cookie.match(reg);
        const arr = matched;
        if (arr) {
          return unescape(arr[2]);
        }
          return null;
      }
      function setCookie(name:string, value:string, time?:number) {
        const Days = time || 30;
        const exp = new Date();
        exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
        document.cookie = `${name}=${escape(value)};expires=${exp.toUTCString()}`;
      }
      if (cookieName && cookieValue) {
        // set cookie
        setCookie(cookieName, cookieValue, day);
        return cookieValue;
      } if (cookieName && cookieValue === null) {
        // delete cookie
        setCookie(cookieName, '', -1);
        return cookieValue;
      } if (cookieName) {
        // read cookie
        return readCookie(cookieName);
      }
      return null;
    }
    
  • 相关阅读:
    关于SVN更新慢的解决方法
    用PS做法线,高光贴图的最简图文教程
    3dmax教程 人物+骨骼+蒙皮+动画教程
    django 模板视图,表单视图,各种视图
    django settings最佳配置
    groovy 弹出菜单
    python construct文档
    用powershell实现自动化操作
    如何用chrome扩展将网页变成黑底白字,用以保护视力
    打造基于CentOS7的xfce最简工作环境
  • 原文地址:https://www.cnblogs.com/everlose/p/12501168.html
Copyright © 2011-2022 走看看