Cookie操作类2(js)

None.gif     < script >
None.gif
function  eyunCookie()
None.gif  {
this .key = "" ; // 初始化key。
None.gif
    this .value = "" ; // 初始化key's value。
None.gif
    this .expires = 0 ; // 初始化cookie的有效时间,单位毫秒。
None.gif
      this .init = function () // 对象初始化
None.gif
                 { this .key = "" ;
None.gif                
this .value = "" ;
None.gif                
this .expires = 0 ;
None.gif                              }
None.gif   
this .set = function (key,value,expires) // 设置cookie
None.gif
              { if ( this .key == "" ) this .key = key;
None.gif                             
if ( this .value == "" ) this .value = value;
None.gif                             
if ( this .expires <= 0 ) this .expires = expires;
None.gif                             
if ( this .key == "" || typeof ( this .key) != " string " )
None.gif                 {alert(
" 请先设置欲保存的cookie名称! " );
None.gif                                  
this .init();
None.gif                  
return   false ;
None.gif                 }
None.gif               
if ( this .key.match( / [,; ] / ))
None.gif                 {alert(
" cookie名称中不能包含“,”、“;”或空格! " );
None.gif                                  
this .init();
None.gif                  
return   false ;
None.gif                 }
None.gif               
if ( this .value.toString().match( / [,; ] / ) || typeof ( this .value) == " undefined " )
None.gif                 {alert(
" cookie值中不能包含“,”、“;”或空格! " );
None.gif                                  
this .init();
None.gif                  
return   false ;
None.gif                 }
None.gif               
if ( this .expires <= 0 || typeof ( this .expires) != " number " )
None.gif                 {alert(
" 请先正确设置cookie的有效时间! " );
None.gif                                  
this .init();
None.gif                  
return   false ;
None.gif                 }
None.gif               
var  cookie = document.cookie;
None.gif               
if (cookie.indexOf( this .key + " = " ) !=- 1 )
None.gif                 {
if ( ! confirm( " 欲保存的cookie名称已经存在,是否要进行替换? " ))
None.gif                                    {
this .init();
None.gif                                         
return   false ;
None.gif                                        }                    
None.gif                 }
None.gif               
var  dt = new  Date();
None.gif               dt.setTime(dt.getTime()
+ this .expires);
None.gif                             document.cookie
= this .key + " = " + this .value + " ;expires= " + dt.toGMTString();
None.gif                             
this .init();
None.gif               
return   true ;
None.gif              }
None.gif   
this .get = function (key) // 取得名为key的cookie的值
None.gif
              { if (key == "" || key.match( / [,; ] / ))
None.gif                 {alert(
" 请正确设置欲查找的cookie名称! " )
None.gif                  
return   false ;
None.gif                 }
None.gif               
var  cookie = document.cookie;
None.gif               
var  start = cookie.indexOf(key + " = " );
None.gif               
if (start ==- 1 )
None.gif                 {alert(
" 欲查找的cookie不存在! " )
None.gif                  
return   false ;
None.gif                 }
None.gif               
var  end = cookie.indexOf( " ; " ,start);
None.gif                             
if (end ==- 1 )
None.gif                               end
= cookie.length;
None.gif               
var  getCookie = cookie.substring(start + key.length + 1 ,end);
None.gif               alert(
" cookie: " + key + " 的值为 " + getCookie);
None.gif               
return  getCookie;
None.gif              }
None.gif   
this .showAll = function (){alert( " 共有以下cookie对:\n " + document.cookie.split( " ; " ).toString().replace( / , / g, " \n " ));} // 显示所有cookie
None.gif
    this .del = function (key) // 删除名为key的cookie
None.gif
              { if (key == "" || key.match( / [,; ] / ))
None.gif                 {alert(
" 请正确设置欲删除的cookie名称! " )
None.gif                  
return   false ;
None.gif                 }
None.gif                             
var  dt = new  Date();
None.gif               dt.setTime(dt.getTime());
None.gif                             document.cookie
= key + " =eyunDelete;expires= " + dt.toGMTString();
None.gif                             
this .init();
None.gif               
return   true ;
None.gif              }
None.gif   
this .destroy = function () // 销毁所有cookie
None.gif
                    { var  dt = new  Date();
None.gif                   dt.setTime(dt.getTime());
None.gif                                     
while (document.cookie != "" )
None.gif                                       document.cookie
= document.cookie + " ;expires= " + dt.toGMTString();
None.gif                                     
this .init();
None.gif                                     
return   true
None.gif                                    }
None.gif  }
None.gif
var  cookieTest = new  eyunCookie()
None.gif
function  settest()
None.gif{cookieTest.key
= " test "
None.gif cookieTest.value
= " ok "
None.gif cookieTest.expires
= 31536000000
None.gif cookieTest.set()
None.gif}
None.gif
</ script >
None.gif
< input type = button onclick = cookieTest.showAll() value = read >< input type = button onclick = " cookieTest.set('a','test',31536000000) "  value = setA >< input type = button onclick = " settest(); "  value = setTest >< input type = button onclick = " cookieTest.destroy() "  value = clear >< input type = button onclick = cookieTest.get( " test " ) value = gettest >< input type = button onclick = cookieTest.get( " a " ) value = geta >< input type = button onclick = cookieTest.set( " test " , 1 , 31536000000 ) value = resetTest >< input type = button onclick = cookieTest.del( " test " ) value = delTest >

转载于:https://www.cnblogs.com/webcool/archive/2005/02/24/108555.html