码迷,mamicode.com
首页 > 编程语言 > 详细

javascript 原生 cookie 处理

时间:2016-04-13 18:30:45      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

来自网络!

function getCookie(name) {
    var start = document.cookie.indexOf(name + "=");
    var len = start + name.length + 1;
    if (( !start ) && ( name != document.cookie.substring(0, name.length) )) {
      return null;
    }
    if (start == -1) return null;
    var end = document.cookie.indexOf(‘;‘, len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len, end));
  }
  function setCookie(name, value, expires, path, domain, secure) {
    var today = new Date();
    today.setTime(today.getTime());
    if (expires) {
      expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));
    document.cookie = name + ‘=‘ + escape(value) +
      ( ( expires ) ? ‘;expires=‘ + expires_date.toGMTString() : ‘‘ ) +
      ( ( path ) ? ‘;path=‘ + path : ‘‘ ) +
      ( ( domain ) ? ‘;domain=‘ + domain : ‘‘ ) +
      ( ( secure ) ? ‘;secure‘ : ‘‘ );
  }

  

javascript 原生 cookie 处理

标签:

原文地址:http://www.cnblogs.com/zlog/p/5387937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!