JS写的cookie类

JS cookie 操作工具类 JS cookie 操作工具类,对cookie的设置 读取 删除 ,有效期的设置等 立即下载

JS写的cookie类



<script language="JScript">
function Cookie(delim){
 this._Cookie=[];
 this.Load=function(){
 if(document.cookie.indexOf(";")!=-1){
  var _sp,_name,_tp,_tars,_tarslength;
  var _item=document.cookie.split("; ");
  var _itemlength=_item.length;
  while(_itemlength>0){
   _sp=_item[--_itemlength].split("=");
   _name=_sp[0];
   _tp=_sp[1].split(",");
   _tars=_tp.slice(1,_tp.length);
   this._Cookie[_name]=[];
   this._Cookie[_name]=_tars;
   this._Cookie[_name]["timeout"]=_tp[0];
  }
  return true;
 }
 return false;
 }
 this.Save=function(){
 var _str,_ars,_mars,_marslength,timeout,i,key;
 for(key in this._Cookie){
  if(!this._Cookie[key])return;
  _str=[];
  _mars=CookieClass._Cookie[key];
  _marslength=_mars.length;
  for(i=0;i<_marslength;i++)_str[_str.length]=escape(_mars[i]);
  document.cookie=key+"="+_mars["timeout"]+(_str.length>0?",":"")+_str+(_mars["timeout"]==0?"":";expires="+new Date(parseInt(_mars["timeout"])).toGMTString());
 }

 }
 this.GetCookieCount=function(){
 var _length=0,key;
 for(key in this._Cookie)_length++;
 return _length;
 }
 this.Create=function(name,days){
 days=days?days:0;
 if(!this._Cookie[name])this._Cookie[name]=[];
 this._Cookie[name]["timeout"]=days!=0?new Date().getTime()+parseInt(days)*86400000:0;
 }
 this.Modify=function(name,days){
 this.Create(name,days);
 }
 this.GetTime=function(name){
 return new Date(parseInt(this._Cookie[name]["timeout"]));
 }
 this.Delete=function(name){
 this.Create(name,0);
 }
 this.AddItem=function(name,value){
 this._Cookie[name][this._Cookie[name].length]=value;
 }
 this.DelItem=function(name,index){
 var _ttime=this._Cookie[name]["timeout"];
 this._Cookie[name]=this._Cookie[name].slice(0,index).concat(this._Cookie[name].slice(parseInt(index)+1,this._Cookie[name].length));
 this._Cookie[name]["timeout"]=_ttime;
 }
 this.GetCount=function(name){
 return this._Cookie[name].length;
 }
 this.GetItem=function(name,index){
 return this._Cookie[name][index];
 }
}
</script>
<script language="JScript">
//Cookie类测试
var CookieClass=new Cookie();
if(!CookieClass.Load()){
CookieClass.Create("Pass",1);
CookieClass.AddItem("Pass","Ps1");
CookieClass.AddItem("Pass","Ps2");
CookieClass.AddItem("Pass","Ps3");
CookieClass.AddItem("Pass","Ps4");
CookieClass.DelItem("Pass",1);
CookieClass.Save();
}
alert("Cookie过期时间:"+CookieClass.GetTime("Pass").toLocaleString());
alert(document.cookie);
</script>
<script>
function eyunCookie()
{this.key="";//初始化key。
this.value="";//初始化key's value。
this.expires=0;//初始化cookie的有效时间,单位毫秒。
this.init=function()//对象初始化
{this.key="";
this.value="";
this.expires=0;
}
this.set=function(key,value,expires)//设置cookie
{if(this.key=="")this.key=key;
if(this.value="/="")this.value=value;"
if(this.expires<=0)this.expires=expires;
if(this.key==""||typeof(this.key)!="string")
{alert("请先设置欲保存的cookie名称!");
this.init();
return false;
}
if(this.key.match(/[,; ]/))
{alert("cookie名称中不能包含“,”、“;”或空格!");
this.init();
return false;
}
if(this.value.toString().match(/[,; ]/)||typeof(this.value)=="undefined")
{alert("cookie值中不能包含“,”、“;”或空格!");
this.init();
return false;
}
if(this.expires<=0||typeof(this.expires)!="number")
{alert("请先正确设置cookie的有效时间!");
this.init();
return false;
}
var cookie=document.cookie;
if(cookie.indexOf(this.key+"=")!=-1)
{if(!confirm("欲保存的cookie名称已经存在,是否要进行替换?"))
{this.init();
return false;
}
}
var dt=new Date();
dt.setTime(dt.getTime()+this.expires);
document.cookie=this.key+"="+this.value+";expires="+dt.toGMTString();
this.init();
return true;
}
this.get=function(key)//取得名为key的cookie的值
{if(key==""||key.match(/[,; ]/))
{alert("请正确设置欲查找的cookie名称!")
return false;
}
var cookie=document.cookie;
var start=cookie.indexOf(key+"=");
if(start==-1)
{alert("欲查找的cookie不存在!")
return false;
}
var end=cookie.indexOf(";",start);
if(end==-1)
end=cookie.length;
var getCookie=cookie.substring(start+key.length+1,end);
alert("cookie:"+key+"的值为"+getCookie);
return getCookie;
}
this.showAll=function(){alert("共有以下cookie对:/n"+document.cookie.split(";").toString().replace(/,/g,"/n"));}//显示所有cookie
this.del=function(key)//删除名为key的cookie
{if(key==""||key.match(/[,; ]/))
{alert("请正确设置欲删除的cookie名称!")
return false;
}
var dt=new Date();
dt.setTime(dt.getTime());
document.cookie=key+"=eyunDelete;expires="+dt.toGMTString();
this.init();
return true;
}
this.destroy=function()//销毁所有cookie
{var dt=new Date();
dt.setTime(dt.getTime());
while(document.cookie!="")
document.cookie=document.cookie+";expires="+dt.toGMTString();
this.init();
return true
}
}

//eyunCookie类测试
var cookieTest=new eyunCookie()
function settest()
{cookieTest.key="test"
cookieTest.value="ok"
cookieTest.expires=31536000000
cookieTest.set()
}
</script>
<input type=button onclick=cookieTest.showAll() value=read>
<input type=button onclick="cookieTest.set('a','test',31536000000)" value=setA>
<input type=button onclick="settest();" value=setTest>
<input type=button onclick="cookieTest.destroy()" value=clear>
<input type=button onclick=cookieTest.get("test") value=gettest>
<input type=button onclick=cookieTest.get("a") value=geta>
<input type=button onclick=cookieTest.set("test",1,31536000000) value=resetTest>
<input type=button onclick=cookieTest.del("test") value=delTest>

 

js操作cookies类 js javascript cookie 类 操作 立即下载

相关推荐

2021-3-20组会 无监督训练下的音频分离 (音源分离) Unsupervised Audio Source Separation Using Generative Priors

0. 说明 一片具体论文的综述

ruclion的专栏 1349

JavaScript Cookie 详解

JavaScript 中的 Cookie 是一种用于在浏览器和服务器之间传递数据的机制。它通常用于跟踪用户的会话信息,如登录状态、购物车内容等。

猫老板的豆 1万+

别再乱调了!Zygo MetroPro软件滤波功能保姆级指南,从均值到鲁棒高斯样条一次讲清

本文深入解析Zygo MetroPro软件的滤波功能,从均值滤波到鲁棒高斯样条等九种算法,帮助用户根据测量对象特性选择最佳滤波组合。通过真实工业案例和参数配置指南,提升光学测量数据的准确性和效率,特别适用于硅晶圆、光学镜片等精密测量场景。

weixin_30888413的博客 156

Cookie操作类2(js)

<script> functioneyunCookie() {this.key="";//初始化key。 this.value="";//初始化key'svalue。 this.expires=0;//初始化cookie的有效时间,单位毫秒。 this.init=function()//对象初始化 ...

weixin_30695195的博客 177

FOC电机控制中电流采样延迟补偿的原理和方法

摘要: FOC电机控制中,电流采样、计算与PWM更新存在固有延迟(约1.5个PWM周期),导致相位滞后,限制电流环带宽。主流补偿方法包括: 一阶前向预测法:基于历史电流值预测未来电流,抵消延迟,工程应用广泛; 相位超前补偿:通过s/z域校正器设计实现等效预测; 状态观测器法(如龙伯格观测器):通过电机模型闭环估计状态,兼具噪声抑制和参数辨识能力,但复杂度高。实际需注意参数敏感性、离散化精度及延迟时间校准,避免过补偿风险。一阶预测法因平衡效果与复杂度成为工业首选。

jifengzhiling的博客 2231

黑猫带你学UFS协议第2篇:UFS相关名词释义

本文依据UFSJEDEC3.1、个人工作经验整理而成,如有错误请留言。文章为个人辛苦整理,付费内容,已加入原创保护,禁止私自转载。文章所在专栏《黑猫带你学UFS协议详解》

黑猫奥利奥的博客 5302

JavaScript中的cookie

JavaScript中的cookie 对于一个码农来说,cookie可能是很常用的一个技术点,今天说一下对前端(JavaScript)中的cookie理解和使用心得。 在学习这个知识点前,先将它分为以下几个小节: 什么是cookie,cookie的作用 cookie的工作机制,运作流程 cookie属性项 cookie的作用域 1.什么是cookie? 简单来说,cookie是一个存放数据的东西,存储量4kb,在有效期内存放在客户端上和应用设备上。 举个????:你去银行办一张信用卡,需要身份证,密码,

cxyxiaosong的博客 3861

2. 第一个 SAP CDS View 源代码的详细解析:CDS View 和底层自动生成 SQL View 的关系讲解

笔者前一篇教程文章,带着大家在 ABAP 系统创建了一个最简单的基于sflight数据库表的 CDS View:本文对这简单的 14 行源代码背后的知识点进行详细讲解。CDS 全称是 Core Data Services,它是一种数据建模技术,帮助开发者以更直观和高层的方式去描述数据库中所需的数据结构。在一个典型的 SAP ABAP 开发场景里,CDS 经常被用来编写各种用于报表、接口或业务逻辑的数据视图。通常,这些视图包含一系列注解。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1021

JS写的Cookie类

  1script language="JScript">   2function Cookie(delim){   3    this._Cookie=[];   4    this.Load=function(){   5        if(document.cookie.indexOf(";")!=-1){   6            var _sp,_name,_tp,_tars,_t

ξσ Dicky's Blog σξ 1611

【异常】OpenClaw 连接异常This site can‘t be reached 127.0.0.1 refused to connect Try: Checking the proxy

在访问OpenClaw相关服务时,浏览器提示如下异常信息,无法正常加载页面:Try:补充说明:该报错仅出现于本地访问场景(127.0.0.1为本地回环地址),涉及OpenClaw核心服务的本地连接交互,无外部网络及敏感信息泄露风险。

奶爸的编程之路,也就一周冷个三天 1483

梯度提升决策树GBDT原理与代码实例讲解

梯度提升决策树GBDT原理与代码实例讲解 1.背景介绍 1.1 机器学习发展历程 机器学习是人工智能领域的一个重要分支,旨在使计算机具备学习和改进自身性能的能力。随着大数据时代的到来,海量的数据为机器学习算法提供了广阔的应用空间。在

AI天才研究院 1084

JavaScript写的Cookie类

1<scriptlanguage="JScript">2functionCookie(delim){3this._Cookie=[];4this.Load=function(){5if(document.cookie.indexOf(";")!=-1...

angang6606的博客 131

初识数据之特征选择

【代码】初识数据之特征选择。

M_inherit的博客 1134

【转】JavaScript写的Cookie类

在网中转载泛滥且不注明出处的一篇文章,我费了九牛二虎之力才找到原文地址(至少从日期时间和网站的可信度考虑,应该是原文地址),本随笔转载的原文地址:http://blog.youkuaiyun.com/feng_sundy/archive/2005/05/20/376821.aspx 下面给出JavaScript的代码: JavaScript代码用来控制Cookie <scr...

weixin_30687587的博客 79

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

JavaScript写的cookie类

Eclipse.org Software User Agreement Eclipse.org Software User Agreement 15th June, 2011 function Cookie(delim){ this._Cookie

萧逸闲侃 454

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

JavaScript的Cookie类

/*JavaScript的Cookie类注:js设定cookie的时间是以毫秒为单位*/function jsCookie(){ var COOKIE_LIFE ={year:31536000,month:2592000,week:604800, day:86400,hour:3600,browser

shishengsoft的专栏 1264

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

js写cookie到指定域名,js设置cookie domain

input type="submit" name="Submit" value="看Cookie内容" onClick="ReadCookie (document.myform.text1.value)">//函数:取得form表单域的值作为cookie的相关值(cookie name,cookie value,expires)//函数:写入cookie。

chatgpt002的博客 2189
上一篇: CodeSmith技术原理初探
下一篇: 用鼠标拖动表格进行排序.
SmartJavaer
博客等级 码龄21年 13粉丝 37原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值