有一个文本框 , 有一个提交按钮,  文本框里面写一些 数据, 点击 提交按钮 , 就 将数据保存到  cookie1 里面 , 在文本框写一些 字符串, 点击 提交按钮,就 将 数据 保存到 cookie2 里面  ....   重复 10次 , cookie 里面 就有 10个记录 ,   当 再次 输入值,   点击 提交 按钮 的时候,  就 将 值 赋值给 cookie1 里面,  然后  cookie1 替换 cookie2  , cookie2  替换  cookie3   ,  如此 重复替换 ...      ,  替换到最后的时候,  cookie10 里面 就是 cookie9 的值 ,  
    
       不知道我说的意思 , 懂不懂.. ?     

解决方案 »

  1.   

    //写cookies函数
    function SetCookie(name,value)//两个参数,一个是cookie的名子,一个是值
    {
        var Days = 30; //此 cookie 将被保存 30 天
        var exp  = new Date();    //new Date("December 31, 9998");
        exp.setTime(exp.getTime() + Days*24*60*60*1000);
        document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
    }
    function getCookie(name)//取cookies函数        
    {
        var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
         if(arr != null) return unescape(arr[2]); return null;}
    function delCookie(name)//删除cookie
    {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval=getCookie(name);
        if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
    }SetCookie ("xiaoqi", "3")
    alert(getCookie('xiaoqi'));
    </script>
    读写Cookie函数function  GetCookieVal(offset)
    //获得Cookie解码后的值
    {
    var  endstr  =  documents.cookie.indexOf  (";",  offset);
    if  (endstr  ==  -1)
    endstr  =  documents.cookie.length;
    return  unescape(documents.cookie.substring(offset,  endstr));
    }
    function  SetCookie(name,  value)
    //设定Cookie值
    {
    var  expdate  =  new  Date();
    var  argv  =  SetCookie.arguments;
    var  argc  =  SetCookie.arguments.length;
    var  expires  =  (argc  >  2)  ?  argv[2]  :  null;
    var  path  =  (argc  >  3)  ?  argv[3]  :  null;
    var  domain  =  (argc  >  4)  ?  argv[4]  :  null;
    var  secure  =  (argc  >  5)  ?  argv[5]  :  false;
    if(expires!=null)  expdate.setTime(expdate.getTime()  +  (  expires  *  1000  ));
    documents.cookie  =  name  +  "="  +  escape  (value)  +((expires  ==  null)  ?  ""  :  (";  expires="+  expdate.toGMTString()))
    +((path  ==  null)  ?  ""  :  (";  path="  +  path))  +((domain  ==  null)  ?  ""  :  (";  domain="  +  domain))
    +((secure  ==  true)  ?  ";  secure"  :  "");
    }
    function  DelCookie(name)
    //删除Cookie
    {
    var  exp  =  new  Date();
    exp.setTime  (exp.getTime()  -  1);
    var  cval  =  GetCookie  (name);
    documents.cookie  =  name  +  "="  +  cval  +  ";  expires="+  exp.toGMTString();
    }
    function  GetCookie(name)
    //获得Cookie的原始值
    {
    var  arg  =  name  +  "=";
    var  alen  =  arg.length;
    var  clen  =  documents.cookie.length;
    var  i  =  0;
    while  (i  <  clen)
    {
    var  j  =  i  +  alen;
    if  (documents.cookie.substring(i,  j)  ==  arg)
    return  GetCookieVal  (j);
    i  =  documents.cookie.indexOf("  ",  i)  +  1;
    if  (i  ==  0)  break;
    }
    return  null;
    }
    <SCRIPT  language="javascript">
    <!--
    function  openpopup(){
    url="popup.htm"
    window.open("gonggao.htm","gonggao","width=260,height=212,left=200,top=0")
    }function  get_cookie(Name)  {
    var  search  =  Name  +  "="
    var  returnvalue  =  "";
    if  (documents.cookie.length  >  0)  {
    offset  =  documents.cookie.indexOf(search)
    if  (offset  !=  -1)  {
    offset  +=  search.length
    end  =  documents.cookie.indexOf(";",  offset);
    if  (end  ==  -1)
    end  =  documents.cookie.length;
    returnvalue=unescape(documents.cookie.substring(offset,  end))
    }
    }
    return  returnvalue;
    }function  helpor_net(){
    if  (get_cookie('popped')==''){
    openpopup()
    documents.cookie="popped=yes"
    }
    }
    helpor_net()
    //-->
    </SCRIPT><SCRIPT LANGUAGE="JavaScript">
    <!--
    var the_cookie = document.cookie;
    var broken_cookie = the_cookie.split(":");
    var the_visiteraccepted = unescape(broken_cookie[1]);
    //
    if (the_visiteraccepted=="undefined"){
            var tmp=confirm('中国人何时何地。');
            if(tmp==false){
                    window.close();
            }else{
                    var the_visiteraccepted = 1;          
            var the_cookie = "ILoveChina=visiteraccepted:" + escape(the_visiteraccepted);                                 
            document.cookie = the_cookie;
            }
    }
    //-->
    </SCRIPT>
    网上找一找Cookie的读写。