js操作Cookies存取数组形式的实例分享

发布时间:2020-12-04编辑:脚本学堂
本文介绍下,js操作cookies,以数据形式存取的一例代码,分享一下,有需要的朋友作个参考。

代码如下:
 

复制代码 代码示例:
<html> 
<head> 
<title>js操作cookie 数组形式_www.jb200.com</title> 
<script type="text/javascript" language="javascript" src="http://img.jb200.com/js/jquery-1.4.2.min.js" ></script> 
</head> 
<body> 
<script> 
//cookie相关函数 
// ------------ 
 var CookieCars = { 
    setCookie: function(name, value) { 
        expiryday = new Date(); 
        expiryday.setTime(expiryday.getTime() + 30 * 30 * 24 * 60 * 60 * 1 * 1000); 
        document.cookie = name + "=" + escape(value) + "; expires=" + expiryday.toGMTString(); 
    }, 
 
    getCookie: function(name) { 
        var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)")); 
 
        if (arr != null) { 
            return unescape(arr[2]); 
        } 
        return null; 
    }, 
 
    clearCookie: function(name) { 
      var exp = new Date();  
        exp.setTime(exp.getTime() - 1);  
        var cval = CookieCars.getCookie(name);  
        if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();  
    } 
}; 
 
//---------- 
function addCarToYescar(id, name) { 
    var Yescar = CookieCars.getCookie("ActiveNewYescar"); 
    var com_arr = null; 
    if (Yescar) { 
        com_arr = Yescar.split("|"); 
        if (com_arr.length >= 10) { 
            alert("对比车型不能多于10个"); 
         
            return; 
        } 
        if (Yescar.indexOf("id" + id + ",") >= 0) { 
            alert("您选择的车型,已经在对比列表中!"); 
         
            return; 
        } 
    } 
    else { 
        com_arr = new Array(); 
    } 
    com_arr.push('id' + id + ',' + name); 
    CookieCars.clearCookie("ActiveNewYescar"); 
    CookieCars.setCookie("ActiveNewYescar", com_arr.join("|")); 
    showCarsLi() ; 

                        
    function showCarsLi() { 
 
    var Yescar = CookieCars.getCookie("ActiveNewYescar"); 
    //if (!WaitYescarObj.IDListULObj) { 
    //  if (document.getElementById('idListULForWaitYescar')) 
    //  { WaitYescarObj.IDListULObj = document.getElementById('idListULForWaitYescar'); } 
    //  else 
    //  { return; } 
    //} 
    if (Yescar) { 
        $("#div1").html(''); 
        var com_arr = Yescar.split("|"); 
        var tempHTML = new Array(); 
        for (var i = 0; i < com_arr.length; i++) { 
            var id = com_arr[i].split(",")[0].substring(2, com_arr[i].split(",")[0].length); 
            var name = com_arr[i].split(",")[1]; 
            //startCarCsInfoRequestForWaitYescar(id); 
            //if (WaitYescarObj.CsName != "" && WaitYescarObj.CsAllSpell != "") { 
            //  tempHTML.push("<li><a href="http://www.jb200.com/" + WaitYescarObj.CsAllSpell + "/m" + id + "/">" + WaitYescarObj.CsName + " " + name + "</a><div class="bnt_YescarClose" onclick="javascript:delYescar('" + id + "','" + name.replace("'", "’") + "');">删除</div></li>"); 
            //} 
            //else { 
                tempHTML.push("<li><a href="">" + name + "</a><a href="javascript:void('0')"  onclick="javascript:delYescar('" + id + "','" + name.replace("'", "’") + "');">删除</a></li>"); 
            //} 
            // 综述页已添加的变更 
            $("#div1").html(tempHTML.join(""));             
        }     
    } 

 
 function delYescar(id, name) { 
    var Yescar = CookieCars.getCookie("ActiveNewYescar"); 
    com_new_arr = new Array(); 
    if (Yescar) { 
        var com_arr = Yescar.split("|"); 
        for (var i = 0; i < com_arr.length; i++) { 
            if (com_arr[i].indexOf("id" + id + ",") < 0) { 
                com_new_arr.push(com_arr[i]); 
                } 
            } 
        } 
        CookieCars.clearCookie("ActiveNewYescar"); 
        CookieCars.setCookie("ActiveNewYescar", com_new_arr.join("|")); 
        showCarsLi(); 
    } 
 
    $(document).ready(function(){ 
showCarsLi(); 
}); 
 
    function AddSelect()  
    { 
    //alert(obj.id); 
                  //alert($("#"+obj.id+"").val()) 
                  //alert($("#idSelect").text()); 
                 // var item = $("select[@name=items] option[@selected]").text() 
                // alert($("#idSelect option:selected").text()); 
                addCarToYescar($("#idSelect").val(),$("#idSelect option:selected").text()); 
    }
 
    function Clear() 
    { 
        CookieCars.clearCookie("ActiveNewYescar"); 
        showCarsLi(); 
    } 
</script> 
<div id="div1"> 
</div> 
<div> 
<a href="javascript:void(0)" onclick="addCarToYescar('123423','马六11320')" target="_bank" >werwer</a> 
<a href="javascript:void(0)" onclick="addCarToYescar('134123','B5222王子')" target="_bank" >werwer</a> 
<a href="javascript:void(0)" onclick="addCarToYescar('5223','天22朝')" target="_bank" >werwer</a> 
<a href="javascript:void(0)" onclick="addCarToYescar('4122','米22娜')" target="_bank" >werwer</a> 
<select id="idSelect"> 
   <option value="112">脚本学堂</option> 
   <option value="1212">网站编程</option> 
</select> 
<select id="idSelect2"> 
   <option value="3457">php基础教程</option> 
   <option value="1212">linux target=_blank class=infotextkey>linux基础教程</option> 
</select> 
<button type="button" onclick="AddSelect()" class="button">添加</button> 
<input type="button" onclick="Clear()" value="清除"></input> 
</div> 
</body >  
</html>