📄 util.js
字号:
function getCookieVal(offset)
{
var endstr = document.cookie.indexOf(";", offset);
if(endstr == -1)
{
endstr = document.cookie.length;
}
return unescape(document.cookie.substring(offset, endstr));
}
// primary function to retrieve cookie by name
function getCookie(name)
{
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while(i < clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
{
return getCookieVal(j);
}
i = document.cookie.indexOf(" ", i) + 1;
if(i === 0) {break;}
}
return;
}
// store cookie value with optional details as needed
function setCookie(name, value, expires, path, domain, secure)
{
document.cookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
// remove the cookie by setting ancient expiration date
function deleteCookie(name,path,domain)
{
if(getCookie(name))
{
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}
function numberFormat(num,how)
{
num = Math.round(num*Math.pow(10,how))/Math.pow(10,how);
return num;
}
function countTotal()
{
var products = getCookie("shopcart");
var carts = products.split("|");
var total = 0;
for(var i=0;i < carts.length; i++)
{
var infos = carts[i].split(",");
alert($("price"+infos[0]));
var price = $F("price"+infos[0]);
if(price === null)
{
price =0;
}
total += eval(price)*eval(infos[3]);
}
alert(total);
return total;
}
function toCookieString(carts,idx)
{
var re = '';
for(var i = 0;i < carts.length; i++){
if(i!==idx)
{
re += carts[i];
re +='|';
}
}
return re;
}
//buy products action when user clicked 'buy' button
function buyProduct(pid,amt,cid)
{
if(pid==null){return;}
if(amt==null){amt=1;}
if(cid==null){cid=0;}
var cookiestr = getCookie("shopcart");
if(cookiestr==null||cookiestr=="")
{
cookiestr = "";
cookiestr = cookiestr.concat(pid+","+cid+","+amt);
}
else
{
cookiestr = saveProduct(pid,cookiestr,amt,cid);
}
setCookie("shopcart",cookiestr);
window.open("cart.htm");
return cookiestr;
}
function deleteProduct(pid,cid)
{
if(cid == null){cid = 0;}
var products = getCookie("shopcart");
var carts = products.split("|");
var idx = -1;
products="";
for(var i=0;i < carts.length; i++){
var infos = carts[i].split(",");
if(infos[0] == pid && infos[1] == cid)
{
idx = i;
}
else
{
products +=infos[0]+","+infos[1]+","+infos[2];
}
if(i!==(carts.length-1)&&(i!==idx))
{
products +="|";
}
if(i==(carts.length-1)&&(i==idx))
{
products = products.substr(0,products.length-1);
}
//alert(products);
//$("lbTotalPrice").
}
setCookie("shopcart",products);
//$(""+pid).style.display = "none";
//return products;
}
function modifyProduct(pid,amt,cid)
{
if(cid == null){cid = 0;}
if(amt==null){amt=1;}
var products = getCookie("shopcart");
var carts = products.split("|");
products="";
for(var i=0;i < carts.length; i++){
var infos = carts[i].split(",");
if(infos[0] == pid && infos[1] == cid)
{
infos[2] = eval(amt);
}
products +=infos[0]+","+infos[1]+","+infos[2];
if(i!==(carts.length-1))
{
products +="|";
}
//alert(products);
}
setCookie("shopcart",products);
//$(""+pid).style.display = "none";
//return products;
}
function saveProduct(pid,products,amt,cid)
{
if(cid==null){cid=0;}
if(amt==null){amt=1;}
var isFind = false;
var carts = products.split("|");
var i=0;
products="";
for(;i < carts.length; i++){
var infos = carts[i].split(",");
if(infos[0] == pid && infos[1] == cid)
{
infos[2] = eval(infos[2])+eval(amt);
isFind = true;
}
products +=infos[0]+","+infos[1]+","+infos[2];
if(i!==(carts.length-1))
{
products +="|";
}
}
if(!isFind)
{
products = products.concat("|"+pid+","+cid+","+amt);
}
return products;
}
function isCookieEnabled()
{
if(document.cookie.search('JSESSIONID')==-1)
{
return false;
}
return true;
}
function testCookie(url,func)
{
if(func==null)
{
func = "该";
}
if(document.cookie.search('JSESSIONID')==-1)
{
alert("您的浏览器不支持Cookie,无法正常使用"+func+"功能!");
window.location.href = url;
}
}
function compareTo()
{
//alert($F("rpProduct_ctl00_cbProductID"));
var checks = document.getElementsByName("compare$Product");
var count = 0;
var url = "compare.htm?pids=";
for(var i=0;i<checks.length;i++)
{
if(checks[i].checked)
{
count++;
url += checks[i].value + ",";
}
}
url = url.substr(0,url.length-1);
if(count<2)
{
alert("最少选中两个商品进行比较!");
return;
}
if(count>4)
{
alert("最多选中四个商品进行比较!");
return;
}
//alert(url);
window.location = url;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -