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

  • 相关阅读:
    Fast exit from dram self-refresh
    关于Net开发中一些SQLServer性能优化的建议
    收集一些优秀的DoNet开源项目
    收集一些优秀的DoNet开源项目
    收集一些优秀的DoNet开源项目
    LINQ表达式用法整理
    LINQ表达式用法整理
    LINQ表达式用法整理
    SQL 拼接多个字段的值&一个字段多条记录的拼接
    你应该知道的jQuery技巧【收藏】
  • 原文地址:https://www.cnblogs.com/BeanHsiang/p/283145.html
Copyright © 2011-2022 走看看