ASP .NET 三、asp.net+cookie+javascrip制作的购物车

来源:岁月联盟 编辑:exp 时间:2012-05-29

(1)首先,介绍一下javascript操作cookie的基础


//去除空格,回车
String.prototype.Trim = function()
{
    return this.replace(/(^/s*)|(/s*$)/g, "");
}


//设置Cookie
function setCookie(c_name, c_value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);

    document.cookie = c_name + "=" + escape(c_value) +
        ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

//按名字读取Cookie
function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        var m_car=document.cookie.split(';');
        var value=-1;
        for (i = 0; i < m_car.length; i++)
        {
            if(c_name.toString().Trim()==((m_car[i].split('='))[0]).toString().Trim())
            {
                value = (m_car[i].split('='))[1];
            }
           
        }
    }
    return value;
}

 

 

摘自 南南的空间