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;
    }
    
  • 相关阅读:
    使用HTTP协下载文件
    DNS协议 实践
    操作系统学习笔记 线程
    操作系统学习笔记 进程
    操作系统学习笔记 操作系统概述
    操作系统学习笔记 栈
    操作系统学习笔记 概述
    C语言中的fread和fwrite
    【原】python-jenkins信息
    【转】通过python调用jenkins 常用api操作
  • 原文地址:https://www.cnblogs.com/everlose/p/12501168.html
Copyright © 2011-2022 走看看