zoukankan      html  css  js  c++  java
  • Javascript访问Cookie的四个常用方法

    Javascript访问Cookie的四个基本方法无论是在创建页面还是构建对象都会使用到,单独拿出来备用:

    // utility function called by getCookie()
    function getCookieVal(offset) {
      var endstr = document.cookie.indexOf (";", offset);
      if (endstr == -1) {
        endstr = document.cookie.length;
      }
      return unescape(document.cookie.substring(offset, endstr));
    }

    // primary function to retrieve cookie by name
    function getCookie(name) {
      var arg = name + "=";
      var alen = arg.length;
      var clen = document.cookie.length;
      var i = 0;
      while (i < clen) {
        var 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;
    }

    // store cookie value with optional details as needed
    function setCookie(name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape (value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
    }

    // remove the cookie by setting ancient expiration date
    function deleteCookie(name,path,domain) {
      if (getCookie(name)) {
        document.cookie = name + "=" +
          ((path) ? "; path=" + path : "") +
          ((domain) ? "; domain=" + domain : "") +
          "; expires=Thu, 01-Jan-70 00:00:01 GMT";
      }

  • 相关阅读:
    nginx配置
    day5 业务开发中较有用代码
    day4 Vue基础
    npm vue的一些命令
    day3 ES6基础
    python_矩阵的加法和乘法计算(包括矩阵的动态输入,纯列表实现不引入其他模块)
    python_利用元组实现剪刀石头布
    python_整型与IP地址的转换
    python_判断标识符的合法性
    python_生成随机数与列表排序
  • 原文地址:https://www.cnblogs.com/BeanHsiang/p/283145.html
Copyright © 2011-2022 走看看