国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

javascript中方便增刪改cookie的一個(gè)類

2024-05-06 14:21:06
字體:
供稿:網(wǎng)友
主要是通過對(duì)document.cookie字符串的分析來進(jìn)行功能的組裝的。
溫習(xí)一下javascript中對(duì)cookie的操作:
增加cookie可以用document.cookie="userId=111";來實(shí)現(xiàn)
完整版可以用:document.cookie="userId=111;domain=.google.com;path=/;secure=secure;expire="+date.toGMTString();
可以設(shè)置cookie的過期時(shí)間,域名,路徑
需要?jiǎng)h除只要將expire的時(shí)間設(shè)為現(xiàn)在之前就可以了
現(xiàn)在上我修改的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 < cookies.length; 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 != '') {
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 改则县| 福建省| 临沂市| 石阡县| 麻江县| 墨江| 石首市| 泸州市| 华安县| 特克斯县| 康保县| 库伦旗| 乾安县| 丰城市| 甘泉县| 五河县| 苏尼特右旗| 论坛| 平安县| 余庆县| 佳木斯市| 资兴市| 平泉县| 西昌市| 疏附县| 陇西县| 龙口市| 城口县| 紫金县| 宣城市| 定西市| 吴忠市| 象山县| 读书| 梨树县| 威信县| 江川县| 宁明县| 沙湾县| 沾化县| 托里县|