• [转]Javascript 操作Cookies类


    //原贴 http://bbs.bc-cn.net/dispbbs.asp?BoardID=218&ID=94193
    //
     JavaScript Document
    document.xCookie=Class.create();
    Object.extend(document.xCookie,{
        load: 
    function(d) {
            
    this.$document=|| document;
            
            
    var cookies=this.$document.cookie;
            
    var cookieRegEx=/\w+=[^;]+/g;
            
    var matchs=cookies.match(cookieRegEx);
            
            
    for (var i=0; matchs && i<matchs.length; i++)
            {
                
    var arr=matchs[i].split('=');
                
    this[arr[0]]=new document.xCookie.cookie(this.$document,arr[0],arr[1]);
            }
        },
        
        save: 
    function() {
            
    for (var prop in this)
            {
                
    if ((prop.charAt(0)!='$') && ((typeof this[prop])!='function') && this[prop].write) 
                    
    this[prop].write();
            }
        },
        
        clear: 
    function() {
            
    for (var prop in this)
                
    if (this[prop].remove) this[prop].remove();
        },
        
        $: 
    function(name) {
            
    if (this[name])
                
    return this[name];
            
    else
            {
                
    this[name]=new document.xCookie.cookie(this.$document,name);
                
    return this[name];
            }
        }
    });


    document.xCookie.cookie
    =function(document,name,val,timeout,path,domain,secure) {
        
    this.$document=document;
        
    this.$name=name;
        
    if (val) 
            
    this.$val=val;
        
    else
            
    this.$val='';
        
    if (timeout)
            
    this.$expiration=new Date((new Date()).getTime()+timeout*60000);
        
    else
            
    this.$expiration=null;
        
    if (path)
            
    this.$path=path;
        
    else
            
    this.$path=null;
        
    if (domain)
            
    this.$domain=domain;
        
    else
            
    this.$domain=null;
        
    if (secure)
            
    this.$secure=true;
        
    else
            
    this.$secure=false;
        
    this.read();
    }

    document.xCookie.cookie.prototype.read
    =function() {
        
    var valRegEx=/\w+:[^&]+/g;
        
    var matchs=this.$val.match(valRegEx);
        
        
    for (var i=0;matchs && i<matchs.length; i++)
        {
            
    var arr=matchs[i].split(':');
            
    this[arr[0]]=unescape(arr[1]);
        }
    }

    document.xCookie.cookie.prototype.write
    =function() {
        
    var val=this.toString();
        
    var cookie=this.$name+'='+val;
        
    if (this.$expiration) cookie+=";expires="+this.$expiration.toGMTString();
        
    if (this.$path) cookie+=";path="+this.$path;
        
    if (this.$domain) cookie+=";domain="+this.$domain;
        
    if (this.$secure) cookie+=";secure";
        
    this.$document.cookie=cookie;
    }

    document.xCookie.cookie.prototype.remove
    =function() {
        
    var val='';
        
    for (var prop in this)
        {
                
    if ((prop.charAt(0)=='$')||((typeof this[prop])=='function'))
                    
    continue;
                
    this[prop]=null;
        }
        
    this.$expiration=new Date(1970,1,2,0,0,0);
    }

    document.xCookie.cookie.prototype.toString
    =function() {
        
    var val='';
        
    for (var prop in this)
        {
                
    if ((prop.charAt(0)=='$')||((typeof this[prop])=='function'))
                    
    continue;
                
    if (val!='') val+='%';
                val
    =prop+':'+escape(this[prop]);
        }
        
    return val;
    }

    //var xCookie=Class.create();
    //
    Object.extend(xCookie,document.xCookie);



    对象:document.xCookie / xCookie (两个对象同等)
    方法:
            document.xCookie.load(d)   预加载当前已存cookies,参数d为文档对象,默认为当前文档,即document。此函数是预加载,需在所有cookie操作前使用。
            document.xCookie.save() 保存对cookies的修改
            document.xCookie.clear() 清除当前文档所有cookies
            document.xCookie.$(name) 返回以name命名的cookie对象(document.xCookie.cookie类),相当于asp里的Response.Cookies(name)。用点语法或[]语法访问该cookie的属性值,即document.xCookie.$(name).prop或document.xCookie.$(name)[prop]。如果属性值不存在,则返回undefined(null)


    类:document.xCookie.cookie (操作以name命名的cookie对象,document.xCookie.$(name)返回的就是这个类的实例,所以这个类的实例可以不必单独自行创建)
    构造函数:
            function(document, name, timeout, path, domain, secure),document为文档对象(必填);name为cookie的名字(必填);timeout为cookie过时设置,单位为分钟,可缺省,默认为暂时保存;后三个参数不在赘述,不明白的请参考有关javascript里的cookie知识,可缺省。
    方法:
            class.read(),读取当前cookie的属性值对(创建类实例时已经执行)
            class.write(),写入对当前cookie的修改
            class.remove(),删除当前cookie
            class.toString(),返回属性值对的字符串表示形式
    属性:
            class.prop,访问当前cookie的prop属性

    举例:
            document.xCookie.load();
            document.xCookie.$('myname').first='terry';
            document.xCookie.$('myname').last='king';
            document.xCookie.$('sex').sex='male';       
            document.xCookie.save();
            alert(document.xCookie.$('myname').toString());
            alert(document.xCookie.$('sex').toString());

    备注,压缩包里一共有两个源文件,xCookie+prototype.js是沿用了prototype(1.4)框架的一些方法,需要prototype框架的支持
    另外一个xCookie是没有沿用prototype框架的。

  • 相关阅读:
    [ jquery 文档处理 replaceWith(content|fn) replaceAll(content) ] 此方法用于把所有匹配的元素替换成指定的HTML或DOM元素
    [ jquery 文档处理 wrapInner(htm|element|fnl) ] 此方法用于把所有匹配的元素的子元素(包括文本节点)使用指定的 HTML 元素来包裹
    [ jquery 文档处理 wrapAll(html|ele)) ] 此方法用于把所有匹配的元素使用指定的 HTML 元素来包裹
    [ jquery 文档处理 wrap(html|element|fn) ] 此方法用于把所有匹配的元素用其他元素的结构化标记包裹起来
    [ jquery 文档处理 insertBefore(content) before(content|fn) ] 此方法用于把所有匹配的元素插入到另一个、指定的元素元素集合的前面,实现外部插入
    [ jquery 文档处理 insertAfter(content) after(content|fn) ] 此方法用于把所有匹配的元素插入到另一个、指定的元素元素集合的后面,实现外部插入
    [ jquery 文档处理 prependTo(content) ] 此方法用于把所有匹配的元素前置到另一个、指定的元素元素集合中
    剑指 Offer 55
    剑指 Offer 54. 二叉搜索树的第k大节点 树的遍历
    剑指 Offer 52. 两个链表的第一个公共节点 哈希
  • 原文地址:https://www.cnblogs.com/skyakira/p/576257.html
Copyright © 2020-2023  润新知