JSONCookie 操作

 

需要使用jquery.jsoncookie.js

           jquery.cookie.js

 

 

var COOKIE_NAME = "Bind_Prod_List";
var product;
 product = $.JSONCookie(COOKIE_NAME);


//判断是否存在
function IsExistsInCookie(userName) {
    var flag = false;
    for (var i = 0; i < product.Product.length; i++) {
        var p = product.Product[i];
        if (p.UserName == userName ) {//存在
            flag = true;
            break;
        }
    }
    return flag;
}
//获取cookie
function GetCookie(uid) {
    if (product.Product) {
        for (var i = 0; i < product.Product.length; i++) {
            var p = product.Product[i];
            if (p.Uid == uid || p.UserName == uid) {
                return p;
                break;
            }
        }
    }
}
//写入cookie
function SetCookie(userName, password, type, uid, group, pid) {
    if (typeof (product.Product) == "undefined") {
        product = { "Product": [{ "UserName": "" + userName + "", "PassWord": "" + password + "", "Type": "" + type + "", "Uid": "" + uid + "", "Group": "" + group + "", "Pid": "" + pid + ""}] };
        $.JSONCookie(COOKIE_NAME, product, { path: '/', expires: timeOut });
        if (ex == "1")
            GetUserStockList(userName, password, type, uid, group, pid);
    } else {
        if (!IsExistsInCookie(userName, password, type, uid)) {
            var json = { "UserName": "" + userName + "", "PassWord": "" + password + "", "Type": "" + type + "", "Uid": "" + uid + "", "Group": "" + group + "", "Pid": "" + pid + "" };
            product.Product.push(json); //添加
            $.JSONCookie(COOKIE_NAME, product, { path: '/', expires: timeOut });
        } 
    }
    ShowList();   
}

//显示列表
function ShowList() {
    var html = "";
    var typeName = "";
    var src = "";
    if (product.Product) {
        $.each(product.Product, function (index, prod) {
            switch (prod.Type) {
               
            }
            html += "<li><img src='" + src + "' />&nbsp;" + typeName + ":<a href='javascript:void(0)' onclick=\"GetUserStockList('" + prod.UserName + "','" + prod.PassWord + "','" + prod.Type + "','" + prod.Uid + "','" + prod.Group + "','" + prod.Pid + "')\">" + prod.UserName + "</a></li>"
        });
    }
    html += productHtml;
    $("#content").html(html);
}

 

posted @ 2012-04-11 17:01  晓风拂月  阅读(3964)  评论(4编辑  收藏  举报