答案:cookie的功能与购物车
zif 发表于2000-01-11 16:13:00
在网上闲逛,发现一好东东,想法不错的!
可在小型的网上商店中使用.
本人在下面列出源代码,大家好好利用!
好什么心得,告诉我一下哦!
------------------------------------
products.html
<html>
<head>
<title>Claremont Toyworld</title>
<script LANGUAGE="javascript">
function SetCookie(name, price, form) {
documents.cookie= documents.cookie + ":" + name + ":";
documents.cookie= documents.cookie + "" + ":";
documents.cookie= documents.cookie + price + ":";
documents.cookie= documents.cookie + "" + ":";
documents.cookie= documents.cookie + "|" + ";";
}
</script>
</head>
<body text="#000000" bgcolor="#FFFF00" link="#0000EE" vlink="#551A8B" alink="#FF0000">
<a href=><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=><td><form>
<p><input TYPE="BUTTON" VALUE="Add to cart" NAME="Additem1"
onclick="SetCookie('Furby','69.95', this.form);"></p>
</form>
</td>
</tr>
</table>
</body>
</html>
----------------------------------------
trolley.html
<html>
<script LANGUAGE="javascript" src="trolley.js">
</script>
</head>
<body>
<table COLS=2 WIDTH="100%" >
<tr>
<td>
</td>
</tr>
</table>
<form name="TrolleyContents" ACTION="formmail.pl" METHOD="POST" ><input type="hidden" name="email" value="email">
<script LANGUAGE="javascript">
tableOfNRows(NumberOfItems(documents.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=>
<td> </td>
</tr>
</table>
<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?"))
{
documents.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 + "|";
documents.cookie = tempi + temp_end;
// alert(documents.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,documents.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,documents.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,documents.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) + ", documents.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
/
上一个:暑期缤纷巨献之~------超级+无敌--------XML+javascript+ASP在线短信息功能!(7)
下一个:以前搜集的一些资料---如何使用asp在自己的网站建立投票机制(2)