日期:2014-05-16  浏览次数:20355 次

请教下用js如何清除cookie?
如,我想在网页上设置按钮“清除cookie”,当点击此按钮后,就清除名为了test的cookie所有值!

------解决方案--------------------
JScript code

var Cookie=new Object(); 
Cookie.setCookie=function(name, value, option){ 
    var str=name+'='+escape(value); 
    if(option){ 
        if(option.expireHours){ 
            var d=new Date(); 
            d.setTime(d.getTime()+option.expireHours*3600*1000); 
            str+='; expires='+d.toGMTString(); 
        } 
        if(option.path) str+='; path='+option.path; 
        if(option.domain) str+='; domain='+option.domain; 
        if(option.secure) str+='; true'; 
    } 
    document.cookie=str; 
} 
Cookie.getCookie=function(name){ 
    var arr = document.cookie.split('; '); 
    if(arr.length==0) return ''; 
    for(var i=0; i <arr.length; i++){ 
        tmp = arr[i].split('='); 
        if(tmp[0]==name) return unescape(tmp[1]); 
    } 
    return ''; 
} 
Cookie.delCookie=function(name){ 
    this.setCookie(name,'',{expireHours:-1}); 
} 

/**How to use it 
Cookie.setCookie('own','this is cookie test!'); 
alert(Cookie.getCookie('own')); 
Cookie.delCookie('own'); 
alert(Cookie.getCookie('own')); 
**/