JavaScript操作Cookie方法实例分析

 更新时间:2015年05月27日 17:31:26   作者:不吃皮蛋  
这篇文章主要介绍了JavaScript操作Cookie方法,实例分析了javascript针对cookie操作的相关技巧,需要的朋友可以参考下

本文实例讲述了JavaScript操作Cookie方法。分享给大家供大家参考。具体如下:

// My methods for setting, reading and deleting cookies.
// I have methods to check for the existence of cookie names or values, 
// to retrieve by name or value, and to create a formatted string of 
// all the cookies.
// My site: andrew.dx.am
var SetCookie = function (name, value, expires, path, domain, secure) {
  // The caller should Trim the name/value pair, if required.
  // Sets the name/value pair (encoded); 'expires' is the no. of days.
  var expires_date;
  if (expires) {
    expires_date = new Date();
    expires_date.setDate(expires_date.getDate() + expires);
  }
  document.cookie = encodeURIComponent(name) + "=" + 
    encodeURIComponent(value) +
    ( ( expires ) ? ";expires=" + expires_date.toUTCString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
};
var DeleteCookie = function (name, path, domain) {
  // The caller should Trim the name/value pair.
  // Encodes the name before deleting.
  document.cookie = encodeURIComponent(name) + "=" + 
    ( ( path ) ? ";path=" + path : "") + ( ( domain ) ? ";domain=" + 
      domain : "" ) + ";expires=Fri, 01-Jan-2010 00:00:01 UTC";
};
var DelAllCookies = function () {
  var currDate = new Date(), i, theCookie = document.cookie.split(";");
  currDate = currDate.toUTCString();
  i = theCookie.length;
  while ( i-- ) {
    document.cookie = theCookie[i] + "; expires =" + currDate;
  }
};
var EscapeReg = function (str) {
  // Helper fn: Escapes characters for use in a regular expression.
  return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};
// The following four functions do not Trim the name or value 
// - the calling fns should do this.
var CNameExists = function (cookie_name) { // case-insensitive
  var testName, myReg;
  if (document.cookie.length == 0) return false;
  testName = EscapeReg(cookie_name);
  myReg = new RegExp('(^|;) ?' + testName + '=([^;]*)(;|$)','i');
  return myReg.test(decodeURIComponent(document.cookie));
};
var CValueExists = function (cookie_value)  { // case insensitive
  var testName, myReg;
  if (document.cookie.length == 0) return false; 
  testName = EscapeReg(cookie_value);
  myReg = new RegExp('(=)' + testName + '(;|$)','i');
  return myReg.test(decodeURIComponent(document.cookie));
};
var CNameGet = function (cookie_value) { // case-insensitive
  var testName, myReg, results;
  if (document.cookie.length == 0) return '';
  testName = EscapeReg(cookie_value);
  myReg = new RegExp('(^|;) ?([^=]*)=' + testName + '(;|$)','i');
  results = decodeURIComponent(document.cookie).match(myReg);
  return ( results ) ? results[2] : '';
};
var CValueGet = function (cookie_name) { // case-insensitive
  var testName, myReg, results;
  if (document.cookie.length == 0) return '';
  testName = EscapeReg(cookie_name);
  myReg = new RegExp('(^|;) ?' + testName + '=([^;]*)(;|$)','i');
  results = decodeURIComponent(document.cookie).match(myReg);
  return ( results ) ? results[2] : '';
};
var CookieStr = function () {
  // Returns a string (with line breaks) which could be 
  // placed in, for example, a textarea.
  return decodeURIComponent(document.cookie).
    replace(/([^=;]+)=([^;]*)[;\s]*/g,'$1 ($2)\n') || '';
};

希望本文所述对大家的javascript程序设计有所帮助。

相关文章

  • 本地存储localStorage用法详解

    本地存储localStorage用法详解

    这篇文章主要为大家详细介绍了本地存储localStorage的用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • 详解js实现线段交点的三种算法

    详解js实现线段交点的三种算法

    下面小编就最近学会的一些”求线段交点”的算法说一说, 希望对大家有所帮助。“求线段交点”是一种非常基础的几何计算, 在很多游戏中都会被使用到。有需要的可以参考学习
    2016-08-08
  • JS基于FileSaver.js插件实现文件保存功能示例

    JS基于FileSaver.js插件实现文件保存功能示例

    这篇文章主要介绍了JS基于FileSaver.js插件实现文件保存功能,结合实例形式演示了FileSaver.js插件的具体使用技巧,需要的朋友可以参考下
    2016-12-12
  • JavaScript实现秒数转换时间的两种格式

    JavaScript实现秒数转换时间的两种格式

    在开发过程中,经常会遇到后台接口返回的是以秒为单位的数据,而我们需要将其转换为一个更加易读的格式,本文将介绍如何实现秒数转时间的两种格式,有需要的可以参考下
    2025-02-02
  • 每个 JavaScript 工程师都应懂的33个概念

    每个 JavaScript 工程师都应懂的33个概念

    这个项目是为了帮助开发者掌握 JavaScript 概念而创立的。它不是必备,但在未来学习( JavaScript )中,可以作为一篇指南,需要的朋友可以参考下
    2018-10-10
  • Threejs与Tween.js结合创建动画的详细图文教程

    Threejs与Tween.js结合创建动画的详细图文教程

    three.js和tween.js可以一起使用,实现复杂的动画效果,包括飞线动画,这篇文章主要给大家介绍了关于Threejs与Tween.js结合创建动画的相关资料,需要的朋友可以参考下
    2024-01-01
  • js实现索引图片切换效果

    js实现索引图片切换效果

    这篇文章主要介绍了js实现索引图片切换效果的代码,特别炫酷的效果,推荐给大家,感兴趣的小伙伴们可以参考一下
    2015-11-11
  • 7道关于JS this的面试题,你能答对几个

    7道关于JS this的面试题,你能答对几个

    这篇文章主要给大家介绍了7道关于JS this的面试题,来看看你能答对几个,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • 一文秒懂js如何快速处理日期以及手写Day.js

    一文秒懂js如何快速处理日期以及手写Day.js

    Day.js是一个轻量的处理时间和日期的JavaScript库,和Moment.js的API设计保持完全一样,这篇文章主要给大家介绍了关于js如何快速处理日期以及手写Day.js的相关资料,需要的朋友可以参考下
    2024-06-06
  • 网页加载时页面显示进度条加载完成之后显示网页内容

    网页加载时页面显示进度条加载完成之后显示网页内容

    网页加载时页面显示进度条(加载完成时显示网页内容),这种效果在浏览网页很常见,本文也介绍一种实现方法,需要了解的朋友可以参考下
    2012-12-12

最新评论