javascript中方便增删改cookie的一个类_javascript技巧
主要是通过对document.cookie字符串的分析来进行功能的组装的。
温习一下javascript中对cookie的操作:
增加cookie可以用document.cookie="userId=111";来实现
完整版可以用:document.cookie="userId=111;domain=.google.com;path=\;secure=secure;expire="+date.toGMTString();
可以设置cookie的过期时间,域名,路径
需要删除只要将expire的时间设为现在之前就可以了
现在上我修改的javascript.cookie.js的类
/*
cookie helper class
easy to write,get,delete
*/
var myCookie={
get:function(name){
if(typeof name != "undefined")
{
//if name given call the get value function
return myCookie_get(name);
}else{
//if name is not given,i want get all the cookie item
return myCookie_getAll();
}
},
add:function(name,value,options){
//write the cookie
myCookie_add(name,value,options);
},
delete:function(name){
//delete the cookie
myCookie_add(name,null);
}
}
String.prototype.Trim = function()
{
return this.replace(/^\s+/g,"").replace(/\s+$/g,"");
}
/*
cookie write function
@name:the cookie name not null
@value:the cookie value null==delete the cookie
@option:{"expires":expire time;"path":/;"domain":localhost;"secure":secure}
*/
function myCookie_add(name,value,options)
{
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
}
var path = options.path ? '; path=' + options.path : '';
var domain = options.domain ? '; domain=' + options.domain : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
}
}
/*
get the name cookie
@name:the cookie's name
*/
function myCookie_get(name)
{
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i var cookie = cookies[i].Trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
/*
get all the cookie return as a json
*/
function myCookie_getAll()
{
var cookieArray = new Array();
var str="";
var temp;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i var cookie = cookies[i].Trim();
temp=cookie.split('=');
//take the
cookieArray.push("{\"name\":\""+decodeURIComponent(temp[0])+"\",\"value\":\""+decodeURIComponent(temp[1])+"\"}");
}
str=cookieArray.join(",");
}
str="["+str+"]";
return eval('('+str+')');
}
调用也是相当简单
myCookie.add("useraccount","admin",{"expires":5});//加入一个期限为5天的cookie
alert(myCookie.get("useraccount"));//取出cookie
cookies=myCookie.get();//得到所有的cookie
for(var i=0;i
alert(cookies[i]["name"]+":"+cookies[i]["value"]);
}
myCookie.delete("useraccount");//删除刚刚添加的cookie
alert(myCookie.get("useraccount"));

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

许多Windows用户最近在尝试访问GoogleChrome浏览器中的网站URL时遇到了一个不寻常的错误,称为Roblox403禁止错误。即使在多次重新启动Chrome应用程序后,他们也无能为力。此错误可能有几个潜在原因,我们在下面概述并列出了其中一些。Chrome的浏览历史和其他缓存以及损坏的数据不稳定的互联网连接网站网址不正确从第三方来源安装的扩展在考虑了上述所有方面之后,我们提出了一些修复程序,可以帮助用户解决此问题。如果您遇到同样的问题,请查看本文中的解决方案。修复1

Cookie通常存储在浏览器的Cookie文件夹中的,浏览器中的Cookie文件通常以二进制或SQLite格式存储,如果直接打开Cookie文件,可能会看到一些乱码或无法读取的内容,因此最好使用浏览器提供的Cookie管理界面来查看和管理Cookie。

电脑上的Cookie存储在浏览器的特定位置,具体位置取决于使用的浏览器和操作系统:1、Google Chrome, 存储在C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default\Cookies中等等。

手机上的Cookie存储在移动设备的浏览器应用程序中:1、在iOS设备上,Cookie存储在Safari浏览器的Settings -> Safari -> Advanced -> Website Data中;2、在Android设备上,Cookie 存储在Chrome浏览器的Settings -> Site settings -> Cookies中等等。

cookie工作原理涉及到服务器发送Cookie、浏览器存储Cookie以及浏览器对Cookie的处理和存储。详细介绍:1、服务器发送Cookie,服务器会向浏览器发送一个包含Cookie的HTTP响应头。这个Cookie包含了一些信息,如用户的身份认证、偏好设置或购物车内容等,浏览器接收到这个Cookie后,会将它存储在用户的计算机上;2、浏览器存储Cookie等等。

随着互联网的普及,我们使用浏览器进行上网已经成为一种生活方式。在日常使用浏览器过程中,我们经常会遇到需要输入账号密码的情况,如网购、社交、邮件等。这些信息需要浏览器记录下来,以便于下次访问时不需要再次输入,这时候Cookie就派上了用场。什么是Cookie?Cookie是指由服务器端发送到用户浏览器上并存储在本地的一种小型数据文件,它包含了一些网站的用户行为

清除cookie产生的影响有重置个性化设置和偏好、影响广告体验、破坏登录状态和记住密码功能。详细介绍:1、重置个性化设置和偏好,如果清除了cookie,购物车将被重置为空,需要重新添加商品,同样清除cookie还会导致在社交媒体平台上的登录状态丢失,需要重新输入用户名和密码;2、影响广告体验,如果清除了cookie,网站将无法了解我们的兴趣和偏好,会展示无关的广告等等。

cookie泄漏的危害有导致个人身份信息被盗窃、个人在线行为被追踪、账户被盗等。详细介绍:1、个人身份信息被盗窃,例如姓名、电子邮件地址、电话号码等,这些信息可能被不法分子用于进行身份盗窃、欺诈等违法活动;2、个人在线行为被追踪,通过分析cookie中的数据,不法分子可以了解用户的浏览历史、购物偏好、兴趣爱好等;3、账户被盗,绕过登录验证,直接访问用户的账号等等。
