操作COOKIE的函数

一个同学慧涛分享给我的他写的操作cookie的函数,贴出来,做个笔记:

 //操作cookie 
    //删除cookie hcookie('cookiename','','del');
    //查询cookie hcookie('cookiename');
    //添加修改cookie hcookie('cookiename','cookievalue')
    function hcookie(key,value,del){
        var v = arguments[1]?arguments[1]:null;
        var d = arguments[1]?arguments[1]:'nodel';
        if(del == "del"){
            var date=new Date(); 
            date.setTime(date.getTime()-10000); 
            document.cookie=key+"="+value+"; expires="+date.toGMTString(); 
            return 'del_ok';
        }else{
            if(value==null){
                var strCookie=document.cookie; 
                var arrCookie=strCookie.split("; "); 
                var result; 
                for(var i=0;i<arrCookie.length;i++){ 
                    var arr=arrCookie[i].split("="); 
                    if(key==arr[0]){ 
                        result=arr[1]; 
                        break; 
                    }
                }
                return result;
            }else{
                document.cookie=key+"="+value;
                return 'add_ok';
            }
        }
    }

 

posted @ 2015-01-12 20:46  suhanyujie  阅读(161)  评论(0编辑  收藏  举报