//JS实现顾客浏览商品的记录以及实现购物车的功能function setCookie(name,value) { var Days = 30; var exp = new Date(); exp.setTime(exp.getTime() + Days*24*60*60*1000); //在最后追加 ;path=/ 非常有必要。否则容易出现 在不同的目录下,调用同一个js方法来存储Cookie,到别的目录取不出或取出的值是不对的这种情况 document.cookie = name + "="+ escape(value) +";expires="+ exp.toGMTString()+";path=/"; //name=value;时间 } function getCookie(name) { var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)")); // name= ; if(arr != null){ alert("我是getCookie()方法:"+unescape(arr[2])); return unescape(arr[2]); } return null; } function delCookie(name) { var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval=getCookie(name); if(cval!=null) document.cookie=name +"="+cval+";expires="+exp.toGMTString(); } /** * 加入购物车,存入cookie */ function goBuy(id, price) { var basePath = document.getElementById("basePath").getAttribute("value"); var newCookie = ""; var oldCookie = getCookie("product"); if(oldCookie) { //如果已经存在在cookie中,则不再添加 if(inArray(oldCookie.split(","), id)) { newCookie = oldCookie; } else { newCookie = id + "," + oldCookie; } } else { newCookie = id; } setCookie("product", newCookie); location.href = basePath+"servlet/CookieServlet?param=car&pid="+id; } /** * 浏览记录,存入cookie */ function goView(id){ var basePath = document.getElementById("basePath").getAttribute("value"); var newCookie = ""; var oldCookie = getCookie("listView"); if(oldCookie) { if(inArray(oldCookie.split(","), id)) { newCookie = oldCookie; } else { newCookie = id + "," + oldCookie; } } else { newCookie = id; } setCookie("listView", newCookie); location.href = basePath+"servlet/CookieServlet?param=listView&pid="+id; }
时间: 2024-10-11 06:12:23