mootools-1.2 学习笔记之cookie操作

    在mootools中支持两种Cookie的读写方式,一种是普通方式,即键值对方式,而另一种是Hash表方式。
    
    第一种方法为:    
  
Cookie.write  //写Cookie
Cookie.read   //读Cookie

    使用方法如下所示:
   
function writeCookie()
{
    
var myCookie = Cookie.write('username''Harald'
);

    
var myCookie  = Cookie.write('username''JackBauer', {duration: 1
});
    
//在另外一个页面上:

    alert(Cookie.read('username'));
    
//移除Cookie

    //Cookie.dispose('username'); //Bye-bye JackBauer! Seeya in 24 Hours.
    
    
//创建一个Cookie并马上移除 它 and Removing it Right Away:

    //var myCookie = Cookie.write('username', 'Aaron', {domain: 'mootools.net'});
    //if (Cookie.read('username') == 'Aaron') { Cookie.dispose(myCookie); }
}
        

    第二种方法为    
  
Hash.Cookie.set //设置,相当于写操作
Hash.Cookie.get //获取,相当于读操作
   
    使用方法如下所示:

function hashCookie()
{
    
var fruits = new Hash.Cookie('myCookieName', {duration: 3600
});
    fruits.extend({
        
'lemon''yellow'
,
        
'apple''red'

    });
    fruits.set(
'melon''green');
    alert(fruits.get(
'lemon')); // yellow

    
    
//在另外一个页面上:

    //var fruits = new Hash.Cookie('myCookieName', {duration: 3600});
    //alert(fruits.get('melon'));     // green
    //fruits.erase();     //  删除cookie
}

    注:本DEMO演示需要在basic_cookie.htm页面进行写入操作,然后再重定向到test.htm页面进行读取操作。
    
    好了,今天的内容就到这里了。
    
    下载DEMO,请
点击这里
    
    tag:mootools, 1.2    
    作者:代震军, daizhj
    原文链接:
http://www.cnblogs.com/daizhj/articles/1291584.html
posted @ 2008-09-16 12:45  代震军  阅读(1740)  评论(0编辑  收藏  举报