亲密接触COOKIE(二):JavaScript 应用篇
/**
* @author 《JavaScript权威指南》蓝文雄解读整理
* 本文档介绍JavaScript对cookie的操作
* 关于基础知识,请参考我的博客http://vincent-lwx.blog.sohu.com/96751243.html或http://www.cnblogs.com/vincent-lwx/archive/2008/08/08/1263803.html
* navigator.cookieEnabled判断浏览器的支持情况
* document.cookie返回所有的cookie对象;
* 使用document.cookie="cookieName="+???+";max-age="+(60*60*24*365);创建名字为cookieName的对象;
* 使用encodeURIComponent(encodedURIString)和decodeURIComponent(encodedURIString)进行编码和解码;
* 文档提供一个工具类,可根据具体cookie存储情况进行修改使用
*/
/**
* Cookie类
* Cookie类的构造函数用来读取指定name的cookie
* Cookie类的store方法用来存储数据,必须先创建对象,然后调用:
* var mycookie=new Cookie("lwx"); mycookie.store();
* Cookie类的remove方法删除对象(通过设置其生命周期),必须先创建对象,然后调用;
* Cookie类的enable方法用来判断浏览器对Cookie的支持情况;
* */
function Cookie(name){
this.$name=name; //设置要查找Cookie对象的名字
var allCookies=document.cookie; //返回所有的cookie对象
if(allCookies=="") return; //在所有cookie列表中如果没有cookie对象
var cookies=allCookies.split(';'); //分离cookies对象,返回cookie对象数组
var cookie=null;
for(var i=0;i<cookies.length;i++) //在数组中查询名字为name的cookie对象
{
if(cookies[i].substring(0,name.length+1)==(name + "=")){
cookie=cookies[i];
break;
}
}
if(cookie==null) return; //没找到指定的cookie对象
var cookieval = cookie.substring(name.length+1); //如果找到cookie对象,开始获取信息
var a=cookieval.split('&'); //返回所有的信息对数组
for(var i=0;i<a.length;i++) //获取信息对的名字
{
a[i]=a[i].split(':');
}
for(var i=0;i<a.length;i++) //获取信息对的值
{
this[a[i][0]]=decodeURIComponent(a[i][1]);
}
}
Cookie.prototype.store=function(daysToLive,path,domain,secure){
var cookieval="";
for(var prop in this)
{
if(prop.charAt(0)=='$'||(typeof this[prop])=='function')
continue;
if(cookieval!="")
cookieval+='&';
cookieval+=prop+':'+encodeURIComponent(this[prop]);
}
var cookie=this.$name+'='+cookieval;
if(daysToLive||daysToLive==0){
cookie +=";max-age="+(daysToLive*24*60*60);
}
if(path) cookie+=";path="+path;
if(domain) cookie+=";domain"+doamin;
if(secure) cookie+=";secure"+secure;
document.cookie=cookie;
}
Cookie.prototype.remove=function(path,domian,secure){
for(var prop in this)
{
if(prop.charAt(0)!='$'&&typeof this[prop]!='function')
delete this[prop];
}
this.store(0,path,domain,secure);
}
Cookie.enbled=function(){ //判断浏览器对cookie的支持与否,返回Boolean值
if(navigator.cookieEnabled!=undefined) return navigator.cookieEnabled; //支持,true
}
附录:一个C#和JavaScript操作Cookie实例
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie aCookie = new HttpCookie("userInfo");
aCookie.Value = "01";
aCookie["username"] = "lwx";
aCookie["lastVisit"] = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);
HttpCookie aCookie1 = new HttpCookie("money");
aCookie1.Value = "02";
aCookie1["much"] = "5000$";
aCookie1["lastVisit"] = DateTime.Now.ToString();
aCookie1.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie1);
}
}
window.onload = function(){
var allCookies=document.cookie; //获取所有Cookie对象,以字符串形式返回
var cookies=allCookies.split(';'); //分离Cookie对象,返回Cookie对象数组
alert("Cookie:"+allCookies);
var cookie=null;
alert("Cookie对象个数:"+cookies.length);
for(var i=0;i<cookies.length;i++) //读取各个Cookie对象
{
cookie=cookies[i];
alert("No "+(i+1)+" Cookie对象:"+cookie);
var a=cookie.split('&'); //分离各个Cookie对象的键值对,返回键值对数组
for(var j=0;j<a.length;j++) //逐个显示Cookie对象的各个键值对
{
b=a[j];
alert("No "+(i+1)+" Cookie对象—No "+(j+1)+"键对:"+b);
}
}
}