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

一个简单的设置和获取cookie值的问题,5555我写不出来了,乱了~~
JS白吃一个,请教大家~~泪奔中~~

JScript code


<ul id="level">
  <li>0</li>
  <li>0</li>
  <li>0</li>
  <li>0</li>
</ul>

<script type="text/javascript">
window.onload=function(){
        function setCookie(cookieName,cookieValue,cookieDate){
            //cookieValue=escape(cookieValue);
            var nowDate=new Date();
                nowDate.setMonth(nowDate.getMonth()+9);
                cookieDate=nowDate.toGMTString();
            document.cookie=cookieName+'='+cookieValue+';expires='+cookieDate;
        }
        
        //setCookie('值','0000','9');
        
        function getCookie(cookiename){
            var strCookie=document.cookie;
            var srtArray=strCookie.split('=');
            var cookieValue=srtArray[1];
            
            return cookieValue;
        }
        
        
        function setValue(){
            var element=document.getElementById('level');
            var tags=element.getElementsByTagName('li');
            var i=0;
            var valueArray=[];
            
            
            if(valueArray!=''){
                valueArray=getCookie('值=').split('');
                for(i=0; i<tags.length; i++){
                    
                    tags[i].index=i;
                    tags[i].innerHTML=valueArray[i];
                }
            }else{
                
                //return null;
            }
        
        }
        
        setValue();
        
        function onSetValue(){
            var element=document.getElementById('level');
            var tags=element.getElementsByTagName('li');
            var i=0;
            var j=0;
            var c=[];
            for(i=0; i<tags.length; i++){
                
                tags[i].index=i;
                tags[i].onclick=function(){
                    this.innerHTML=parseInt(this.innerHTML)+1;
                        
                        
                    var arrayValue=setValue('值=');
                    
                    if(arrayValue==undefined){
                        
                        for(j=0; j<tags.length; j++){
                            
                            c.push(tags.innerHTML);
                        }
                    }
                    setCookie('值',c.splice(this.index,1,this.innerHTML),'9')
                };
            }
        }
        alert(document.cookie);
        onSetValue();
}
</script>




------解决方案--------------------
function init(cookieName,cookieValue,cookieDate){
var a=document.cookie;
var start=a.indexOf(cookieName+"=");
if(start!=-1){
var end=a.indexOf(";",start);
if(end==-1){
var cookies=a.substring(start);
}else{
var cookies=a.substring(start,end);
}
alert(cookies);
}else{
document.cookie=cookieName+"="+cookieValue+";max-age="+parseInt(cookieDate);
}
}
window.onload=function(){init("name","张三"),"100000"};
这样试试
------解决方案--------------------
网络上很多,搜下参考下,理清下思路,再好好写

给你参考下这个:
JScript code

function et_cookie_set(name,value,expires,path,domain){
    var today = new Date();
    if (expires){
        expires = expires * 1000;
    }
    var expires_date = new Date( today.getTime() + (expires) );
    document.cookie = name + '=' +escape( value )
        + ( ( expires ) ? ';expires=' + expires_date.toGMTString() : '' )
        + ( ( path ) ? ';path=' + path : '' )
        + ( ( domain ) ? ';domain=' + domain : '' );
}

function et_cookie_get(name){
      var cookieValue = "