JS实现的购物车
hoppingCart JScript 文件
//----------------
//read COOKIE
function getCookie(cookiename) {
var result;
var mycookie = document.cookie;
var start2 = mycookie.indexOf(cookiename + "=");
if (start2 > -1) {
start = mycookie.indexOf("=", start2) + 1;
var end = mycookie.indexOf(";", start);
if (end == -1) {
end = mycookie.length;
}
result = unescape(mycookie.substring(start, end));
}
return result;
}
//----------------
//write COOKIE
function setCookie(cookiename, cookievalue, hours) {
var date = new Date();
date.setTime(date.getTime() + Number(hours) * 3600 * 1000);
document.cookie = cookiename + "=" + cookievalue + "; path=/;expires = " + date.toGMTString();
}
/*
*取URL传来的参数
*/
function getUrlParams()
{
var id=location.search.split('=')[1];
$("productID").value=id;
}
/*
*加入购物车
*/
/* pID is GameID Amount is BuyNum */
function AddToCart(pID,amount)
{
//setCookie("ShoppingCart","",-1);
var result= getCookie("ShoppingCart");
var bExists=false;
if(result==null)//如果该Cookie为NUll
{
result=pID+"|"+amount;
// setCookie("ShoppingCart",result,1);
// document.location="shopingcart.aspx"; //alert("repeat load:" + result);
}
else
{
var arrResult=result.split(',');
var newCookie="";
for(var i=0;i<arrResult.length;i++)
{
var pid=arrResult[i].split('|')[0];
var amount2=arrResult[i].split('|')[1];
if(pid==pID)
{
bExists=true;
var newAmount= parseInt(amount2)+parseInt(amount);
arrResult[i]=pid+'|'+newAmount;
}
newCookie=newCookie+','+arrResult[i];
}
if(bExists==false)//如果该产品不存在
{
if(result=="")
{
result=pID+"|"+amount;
}
else{
result=result+","+pID+"|"+amount;
}
}
else//已存在
{
result=newCookie.substr(1); //alert("该产品已存在: "+result)
}
}
setCookie("ShoppingCart",result,1);
//document.location="shopingcart.aspx";
window.location="shopingcart.aspx"; // alert("aaa");
}
//Update amount pID是要传过来游戏ID值 ,amount是更改的数值
function UpdateAomnt(pID,amount)
{
//alert("要修改游戏的ID:"+pID);alert("更改的数量:"+ amount);
var result= getCookie("ShoppingCart");
if(result!=null)
{
var arrResult=result.split(',');
var newCookie="";
var j=0;
for(var i=0;i<arrResult.length;i++)
{
var upCookie="";
var pid=arrResult[i].split('|')[0];
if(pid==pID)
{
arrResult[i]=pid+'|'+amount;
upCookie+=arrResult[i]; //alert("修改后的值: "+upCookie);
}
else
{
upCookie+=arrResult[i];
}
newCookie+=","+upCookie;
}
if(newCookie.length>0)
{
result=newCookie.substr(1); //alert("newCookie: "+newCookie);
}
else{
result="";
}
setCookie("ShoppingCart",result,1);
document.location="shopingcart.aspx";
}
}
//Remove ShoppingCart OneGame
function RemoveGame(productID)//productID Value
{
var isOK=confirm('Are you sure you remove?');
if(isOK==false) return;
var result=getCookie("ShoppingCart");
if(result!=null)
{
var arrResult=result.split(',');
var arrResult2=new Array();
var j=0;
for(var i=0;i<arrResult.length;i++)
{
var IdAmount=arrResult[i].split('|');
if(IdAmount[0]!=productID)
{
arrResult2[j]=arrResult[i];
j++;
}
}
var newCookie="";
for(var i=0;i<arrResult2.length;i++)
{
newCookie+=arrResult2[i]+",";
}
if(newCookie.length>0)
{
result=newCookie.substring(0,newCookie.length-1);
}
else
{
result="";
}
setCookie("ShoppingCart",result,1);
document.location="shopingcart.aspx";
}
}