Js对Cookie的操作函数
JavaScript代码
- //写入 cookie
- //不区分 cookieName 的大小写
- //不考虑子键
- function ResponseCookies(cookieName, cookieValue, expires)
- {
- if (expires)
- {
- //指定了 expires
- document.cookie = ResponseCookies_GetCookieName(cookieName) + "=" + escape(cookieValue) + "; expires=" + expires.toGMTString();
- }
- else
- {
- document.cookie = ResponseCookies_GetCookieName(cookieName) + "=" + escape(cookieValue);
- }
- }
- //获取并返回与 cookieName 同名的 cookie 名称,允许大小写不同
- //如果不存在这样的 cookie,就返回 cookieName
- function ResponseCookies_GetCookieName(cookieName)
- {
- var lowerCookieName = cookieName.toLowerCase();
- var cookieStr = document.cookie;
- if (cookieStr == "")
- {
- return cookieName;
- }
- var cookieArr = cookieStr.split("; ");
- var pos = -1;
- for (var i=0; i<cookieArr.length; i++)
- {
- pos = cookieArr[i].indexOf("=");
- if (pos > 0)
- {
- if (cookieArr[i].substring(0, pos).toLowerCase() == lowerCookieName)
- {
- return cookieArr[i].substring(0, pos);
- }
- }
- }
- return cookieName;
- }
- //获取并返回 cookie 值
- //不区分 cookieName 的大小写
- //dfltValue 为默认返回值
- //不考虑子键
- function RequestCookies(cookieName, defValue)
- {
- var lowerCookieName = cookieName.toLowerCase();
- var cookieStr = document.cookie;
- if (cookieStr == "")
- {
- return dfltValue;
- }
- var cookieArr = cookieStr.split("; ");
- var pos = -1;
- for (var i=0; i<cookieArr.length; i++)
- {
- pos = cookieArr[i].indexOf("=");
- if (pos > 0)
- {
- if (cookieArr[i].substring(0, pos).toLowerCase() == lowerCookieName)
- {
- return unescape(cookieArr[i].substring(pos+1, cookieArr[i].length));
- }
- }
- }
- return defValue;
- }
- //--------------------------------------------------------------------
- function WriteCookie()
- {
- var len= arguments.length;
- var domain,name,value,days;
- switch(len)
- {
- case 2:
- domain = "private";
- name = arguments[0];
- value = arguments[1];
- days = 0;
- break;
- case 3:
- domain = "private";
- name = arguments[0];
- value = arguments[1];
- days = arguments[2];
- break;
- default:
- domain = "";
- name = "";
- value = "";
- days = 0;
- break;
- }
- jsWriteCookie(domain,name, value, days);
- }
- function GetCookie(domain,name,defValue)
- {
- var len= arguments.length;
- switch(len)
- {
- case 1:
- domain = "private";
- name = arguments[0];
- defValue = "";
- break;
- case 2:
- domain = "private";
- name = arguments[0];
- defValue = arguments[1];
- break;
- case 3:
- domain = arguments[0];
- name = arguments[1];
- defValue = arguments[2];
- break;
- default:
- domain = "";
- name = "";
- defValue = "";
- break;
- }
- return jsGetCookie(domain,name, defValue);
- }
- function Clear()
- {
- var len= arguments.length;
- var domain,name;
- switch(len)
- {
- case 0:
- domain = "";
- name = "";
- break;
- case 1:
- domain = arguments[0];
- name = "";
- break;
- case 2:
- domain = arguments[0];
- name = arguments[1];
- break;
- default:
- break;
- }
- jsClear(domain,name);
- }
- function jsWriteCookie(domain,name, value, days)
- {
- var expire_date=new Date();
- var ms_from_now=days*24*60*60*1000;
- domain = domain.toLowerCase();
- var domainstr,curhost,cookiedomain;
- curhost = GetHost();
- expire_date.setTime(expire_date.getTime()+ms_from_now);
- if(days == 0)
- {
- document.cookie =encodeURIComponent(domain+name).toLowerCase()+ "=" + encodeURIComponent(value) + "; ; path=/; domain="+curhost+"; ";
- }
- else
- {
- document.cookie =encodeURIComponent(domain+name).toLowerCase() + "=" + encodeURIComponent(value) + "; expires=" + expire_date.toGMTString() + "; path=/; domain="+curhost+"; ";
- }
- }
- function jsGetCookie(domain,name,defValue)
- {
- domain = domain.toLowerCase();
- name = name.toLowerCase();
- var jsmsg="";
- jsmsg += "name="+name+"/r";
- jsmsg += "defValue="+defValue+"/r";
- var search;
- search = encodeURIComponent(domain+name).toLowerCase()+ "="
- jsmsg += "search="+search+"/r";
- offset = document.cookie.indexOf(search)
- if (offset != -1)
- {
- offset += search.length ;
- end = document.cookie.indexOf(";", offset) ;
- if (end == -1)
- end = document.cookie.length;
- return decodeURIComponent(document.cookie.substring(offset, end));
- }
- else
- return defValue;
- }
- function jsClear(domain,name)
- {
- var expdate = new Date();
- expdate.setTime(expdate.getTime() - (86400 * 1000 * 1));
- WriteCookie(name, "", expdate);
- }
- function GetHost(url)
- {
- var host = "";
- if(typeof url == "undefined" || null == url)
- url = window.location.href;
- var regex = /.*/:([^//]*).*/;
- var match = url.match(regex);
- if(typeof match != "undefined" && null != match)
- host = match[1];
- return host;
- }