[摘要]cookie的功能与购物车 zif 发表于2000-01-11 16:13:00 在网上闲逛,发现一好东东,想法不错的! 可在小型的网上商店中使用. 本人在下面列出源代码,大家好好利用! 好什么心得...
cookie的功能与购物车 
zif 发表于2000-01-11 16:13:00 
在网上闲逛,发现一好东东,想法不错的! 
可在小型的网上商店中使用. 
本人在下面列出源代码,大家好好利用! 
好什么心得,告诉我一下哦! 
------------------------------------ 
products.html 
<html> 
<head> 
<title>Claremont Toyworld</title> 
<script LANGUAGE="JAVASCRIPT"> 
function SetCookie(name, price, form) { 
document.cookie= document.cookie + ":" + name + ":"; 
document.cookie= document.cookie + "" + ":"; 
document.cookie= document.cookie + price + ":"; 
document.cookie= document.cookie + "" + ":"; 
document.cookie= document.cookie + " " + ";"; 
} 
</script> 
</head> 
<body text="#000000" bgcolor="#FFFF00" link="#0000EE" vlink="#551A8B" alink="#FF0000"> 
<a href="trolley.html">here.</a> 
<table BORDER="0" CELLSPACING="0" CELLPADDING="0" COLS="3" width="75%"> 
<tr> 
<td><strong>Furby</strong></td> 
<td>The Amazing Pet</td> 
<td><small>AUD</small> $69.95<font size="-2"> + <a href="#Postage">postage</a></font></td> 
<td><form> 
<p><input TYPE="BUTTON" VALUE="Add to cart" NAME="Additem1" 
onclick="SetCookie('Furby','69.95', this.form);"></p> 
</form> 
</td> 
</tr>  
</body> 
</html> 
---------------------------------------- 
trolley.html 
<html> 
<script LANGUAGE="JAVASCRIPT" src="trolley.js"> 
</script> 
</head> 
<body> 
<table COLS=2 WIDTH="100%" > 
<tr> 
<td> 
</td> 
</tr>  
<form name="TrolleyContents" ACTION="formmail.pl" METHOD="POST" ><input type="hidden" name="email" value="email"> 
<script LANGUAGE="JAVASCRIPT"> 
tableOfNRows(NumberOfItems(document.cookie, option_separator, item_separator)); 
</script> 
<table COLS=4 WIDTH="100%" > 
<tr> 
<td><input type="button" value="<-- Back to shopping" onclick="window.close();"></td> 
<td><input type="button" value="Clear shopping trolley" onclick="ClearTrolley();"></td> 
<td> <a href="#Finalise">Finalise order</a></td> 
<td> </td> 
</tr>  
<h1> 
<script LANGUAGE="JAVASCRIPT"> 
totalTable(); 
</script> 
</body> 
</html> 
---------------------------------------- 
trolley.js 
// rem trolley.js source Copyright Prestige Training Centre 
option_separator = ":"; 
item_separator = " "; 
function NumberOfItems(string, option_separating_char, item_separating_char) { 
// returns the number of trolley items 
if(string == "") { 
alert("Your shopping trolley is currently empty."); 
return 0; 
} 
//count the number of item separators 
num_sep_found = 0; 
num_fin_found = 0; 
finished = -1; 
pos = 0; 
while(finished!=1) 
{ 
if(string.charAt(pos) == option_separating_char) num_sep_found++; 
if(string.charAt(pos) == item_separating_char) 
{ 
num_fin_found++; 
if(string.charAt(pos+1) != option_separating_char) finished=1; 
} 
pos++; 
} 
//divide by the number of 'ends' found 
num_sep_found = num_sep_found - num_fin_found; 
//if(num_fin_found >0) return num_sep_found/num_fin_found; 
return num_fin_found; 
} 
function ClearTrolley() { 
if (confirm("Clear the contents of your shopping trolley?")) 
{ 
document.cookie=""; 
history.back(); 
} 
} 
function getElement(terminator,index,string) { 
//find nth terminator 
pos = 0; 
for (inc=1; inc<index+1; inc++) { 
pre_pos = pos+1; 
pos = string.indexOf(terminator,pre_pos); 
} 
//return the substring between the index-1 and index 
temp = string.substring(pre_pos,pos); 
return temp; 
} 
function removeItem(terminator, itemN, string, form ) { 
// removes item from list 
//find nth element 
//find nth terminator 
posi = 0; 
pre_posi=0; 
for (inc=0; inc<itemN; inc++) { 
pre_posi = posi+1; 
posi = string.indexOf(terminator,pre_posi); 
} 
//return the substring between the pre_pos and pos 
// split from 0 to pre_pos 
temp_start = string.substring(0, pre_posi-1); 
// split from pos to end 
temp_end = string.substring(posi+1, string.length); 
// join strings up 
tempi = temp_start; 
if (temp_start !="") tempi = tempi + " "; 
document.cookie = tempi + temp_end; 
// alert(document.cookie); 
// clear the deleted row 
for (inc2=0; inc2<3; inc2++) { 
//alert(); 
form.elements[((itemN-1)*4)+inc2].value=0; 
} 
totalTable(); 
// reload document 
//window.history.go(0); 
} 
function tableOfNRows(n) { 
document.write("<TABLE BORDER COLS=4 WIDTH='100%' >"); 
//write header 
document.write("<TR><TD><CENTER>Item</CENTER></TD><TD><CENTER>Type</CENTER></TD><TD><CENTER>Item Cost<FONT SIZE=-2><BR>(in AUD)</FONT></CENTER></TD>"); 
document.write("<TD></TD></TR>"); 
i = 0; 
do { 
document.write("<TR><TD align=center><INPUT TYPE=TEXT SIZE=15 NAME='item" + i +"'" ); 
document.write(" VALUE='" + getElement(option_separator,(i*5)+1,document.cookie) + "' "); 
document.write("></TD>"); 
document.write("<TD align=center><INPUT TYPE=TEXT SIZE=15 NAME='type" + i + "'" ); 
document.write(" VALUE='" + getElement(option_separator,(i*5)+2,document.cookie) + "' "); 
document.write("></TD>"); 
document.write("<TD><CENTER>$<INPUT TYPE=TEXT SIZE=6 NAME='cost" + i + "'"); 
document.write(" VALUE='" + getElement(option_separator,(i*5)+3,document.cookie) + "' "); 
document.write("></CENTER></TD>"); 
document.write("<TD><INPUT TYPE='BUTTON' VALUE='Remove item' NAME='Remove" + i ); 
document.write("' onclick='removeItem(item_separator," + (i+1) + ", document.cookie, this.form )'></TD></TR>"); 
i++; 
} 
while(i<n) 
//write footer 
document.write("<TR><TD></TD><TD><DIV ALIGN=RIGHT><B>Total =</B></DIV></TD>"); 
document.write("<TD><CENTER>$<INPUT TYPE='TEXT' SIZE='6' NAME='total'></CENTER></TD><TD></TD><TD></TD><TD></TD></TR>"); 
document.write("</TABLE>"); 
} 
function totalTable() { 
//for x = 1 to number of items 
// add all the items offset by n together 
//alert("in totalTable"); 
document.TrolleyContents.total.value = 0; 
offset=6; 
jumper=4; 
items = NumberOfItems(document.cookie, option_separator, item_separator ); 
for (x=0; x<items; x++) 
{ 
index = (x*jumper)+offset; 
jumpertemp=(document.TrolleyContents.elements[index].value) *1; 
if (jumpertemp == 0) items = items+1; 
// if jumpertemp isn't a number then just increase items by one - must be a deleted one 
document.TrolleyContents.total.value= (document.TrolleyContents.total.value*1) + jumpertemp; 
} 
} 
function SendTrolley() { 
temp=""; 
inc=0; 
do { 
//Add name 
temp=temp+"Name = " + document.TrolleyContents.elements[inc].value + " : "; 
inc++; 
//Add type 
temp=temp+"Type = " + document.TrolleyContents.elements[inc].value + " : "; 
inc++; 
//Add price 
temp=temp+"Price = $" + document.TrolleyContents.elements[inc].value + "\n"; 
inc++; 
//Skip remove button 
inc++; 
}while(inc<(document.TrolleyContents.elements.length -10)); 
temp+="\nTotal order price = $" + document.TrolleyContents.elements[inc].value; 
document.TrolleyContents.Items.value = temp; 
} 
--- 
*****@***** 
zif email:zif@163.net 
http://www.xiamencity.com 
*****@*****1999-12-25   
关键词:cookie的技巧与购物车