js操作cookie//设置cookie,参数一:键值,参数二:cookie的值 function setCookie(name,value) { var Days = 1; var exp = new Date(); exp.setTime(exp.getTime() + Days*24*60*60*1000); document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); } //读取cookie function getCookie(name) { var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)"); if(arr=document.cookie.match(reg)) return unescape(arr[2]); else return null; }需要注意下cookie大小的限制以及cookie数量的限制
写入cookie来实现浏览记录的功能asp有写入cookie js也有。
写一个大概的给你,自己再去组织起来, 写入: id=request("id")sql="select * from table where id="&idcname=rs("cname") response.cookie("chanpin")("id"&id&"")=id response.cookie("chanpin")("name""&id&")=cnameif request.cookie("chanpin")("num")="" then response.cookie("chanpin")("num")=id else response.cookie("chanpin")("num")=request.cookie("chanpin")("num")&","&id end if 读取 在这里还要判断下request.cookie("chanpin")("num")是否含有,号,如果没有,就只有一个ID号,直接使用 <a href="showpro.asp?id="&request.cookie("chanpin")("id"&id&"")>request.cookie("chanpin")("name""&id&")</a>就可以了如果含有,号,使用spilt函数分割,再循环读取比如:znum=spilt(request.cookie("chanpin")("num"),",")for i=0 to uBound(znum)-1 zid=znum(i) <a href="showpro.asp?id="&request.cookie("chanpin")("id"&zid&"")>request.cookie("chanpin")("name""&id&")</a> next如果你要限定显示几个 有2种方法,第一种就是在写入COOKIE时,限制response.cookie("chanpin")("num")包含的ID个数,第二种就是限制 for i=0 to uBound(znum)-1这个循环的个数
当然还有别的写法,以上只是写一个你比较容易理解的而已你可以直接使用数组保存 比如 id=request("id") sql="select * from table where id="&id cname=rs("cname") if request.cookie("chanpin")("id")="" then response.cookie("chanpin")("id")=id response.cookie("chanpin")("name")=cname else response.cookie("chanpin")("id")=request.cookie("chanpin")("id")&","&id response.cookie("chanpin")("name")=request.cookie("chanpin")("name")&","&cname end if 也可以使用这个方法 读取的时候使用spilt函数分割一下就可以了
function setCookie(name,value) {
var Days = 1;
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
//读取cookie
function getCookie(name) {
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg)) return unescape(arr[2]);
else return null;
}需要注意下cookie大小的限制以及cookie数量的限制
js也有。
写入:
id=request("id")sql="select * from table where id="&idcname=rs("cname")
response.cookie("chanpin")("id"&id&"")=id
response.cookie("chanpin")("name""&id&")=cnameif request.cookie("chanpin")("num")="" then
response.cookie("chanpin")("num")=id
else
response.cookie("chanpin")("num")=request.cookie("chanpin")("num")&","&id
end if
读取
在这里还要判断下request.cookie("chanpin")("num")是否含有,号,如果没有,就只有一个ID号,直接使用
<a href="showpro.asp?id="&request.cookie("chanpin")("id"&id&"")>request.cookie("chanpin")("name""&id&")</a>就可以了如果含有,号,使用spilt函数分割,再循环读取比如:znum=spilt(request.cookie("chanpin")("num"),",")for i=0 to uBound(znum)-1
zid=znum(i)
<a href="showpro.asp?id="&request.cookie("chanpin")("id"&zid&"")>request.cookie("chanpin")("name""&id&")</a>
next如果你要限定显示几个
有2种方法,第一种就是在写入COOKIE时,限制response.cookie("chanpin")("num")包含的ID个数,第二种就是限制
for i=0 to uBound(znum)-1这个循环的个数
比如
id=request("id") sql="select * from table where id="&id cname=rs("cname")
if request.cookie("chanpin")("id")="" then
response.cookie("chanpin")("id")=id
response.cookie("chanpin")("name")=cname
else
response.cookie("chanpin")("id")=request.cookie("chanpin")("id")&","&id
response.cookie("chanpin")("name")=request.cookie("chanpin")("name")&","&cname
end if 也可以使用这个方法
读取的时候使用spilt函数分割一下就可以了
可不可以麻烦帮忙写个完整点的实例?偶们是超菜级的,实在是搞不明白。如果没时间的话就算了,万分感谢!!!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JavaScriptLab</title>
<style type="text/css">
<!--
body{margin-left:0px;margin-top:10px;margin-right:0px;margin-bottom:10px;}
#div{margin-left:auto;margin-right:auto;width:76%;}
#pbody{border:#88cee9 1px solid;padding:15px;}
-->
</style></head>
<body>
<div id="div">
<div id="pbody">
<div id="txt"></div>
<br/>
<script language="javascript" type="text/javascript">
function setCookie(name,value) {
var Days = 1;
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
} function getCookie(name) {
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
return unescape(arr[2]);
else
return "";
}
function setURL(){
var str=window.location.href;
var re=/Product\d+?.html/;//楼主的静态文件格式是Product数字.html,因此用了这个正则
var arrMatches = str.match(re);
//
if(!(getCookie("test").indexOf(arrMatches)>0)){
if(getCookie("test")!="")
setCookie("test",getCookie("test") + "|" + arrMatches);
else
setCookie("test",arrMatches);
}
document.getElementById("txt").innerHTML = "";
document.getElementById("txt").innerHTML = getCookie("test");
}
</script>
<a href="javascript:setURL();">添加商品</a>
</div>
</div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JavaScriptLab</title>
<style type="text/css">
<!--
body{margin-left:0px;margin-top:10px;margin-right:0px;margin-bottom:10px;}
#div{margin-left:auto;margin-right:auto;width:76%;}
#pbody{border:#88cee9 1px solid;padding:15px;}
-->
</style></head>
<body>
<div id="div">
<div id="pbody">
<div id="txt"></div>
<br/>
<script language="javascript" type="text/javascript">
function setCookie(name,value) {
var Days = 1;
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
} function getCookie(name) {
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
return unescape(arr[2]);
else
return "";
}
function setURL(productId){
//
if(!(getCookie("test").indexOf(productId)>0)){
if(getCookie("test")!="")
setCookie("test",getCookie("test") + "|" + productId);
else
setCookie("test",productId);
}
document.getElementById("txt").innerHTML = "";
document.getElementById("txt").innerHTML = getCookie("test");
}
//document.write(getCookie("test"));
</script>
<a href="javascript:setURL('productId');">添加商品</a>
</div>
</div>
</body>
</html>这个是靠调用函数的时候传递给函数的参数来设置cookie的值的,比上面的那个更灵活一些
/*
存储的信息为:网页连接,产品名称,查看时间
==========其中==========
网页连接:document.location.href
产品名称:通过网页的title获取,所以需要title有比较规范的格式或者title就是产品名称,例子假设title就是产品名称(document.title)===============
你只需要把这个js文件保存起来,然后放到htm页面中就可以了,需要调整的是显示位置,看示例
*/
String.prototype.trim=function(reg)
{
if(reg)
return this.replace(reg,"");
else
return this.replace(/^\s*|\s*$/g,"");
}
function CookieOp()
{
this.MaxCount=4;//默认存储4个产品
this.expires=30;//存活期限30天
this.CIObj=new CookieObj();//cookie对象
this.CIObj.Init();//初始化
this.AddObj=function()
{
var o=this.FindCIObj(document.location.href);
if(o==null)
{
//========创建对象时title你需要根据你的来获取产品名称
if(this.CIObj.Product.length<this.MaxCount)
this.CIObj.Product.push(this.CIObj.createObj(document.location.href,document.title));
else
{
this.CIObj.sort();//先排序
this.CIObj.Product[this.CIObj.Product.length-1]=this.CIObj.createObj(document.location.href,document.title);//更新最后一个为当前对象
}
}
else
o.date=new Date();//更新浏览时间
this.Save();//保存cookie
}
this.Save=function()
{
var d=new Date();
d.setDate(d.getDate()+this.expires);
document.cookie="recent="+this.CIObj.toString()+";expires="+d.toGMTString();
}
this.FindCIObj=function(href)
{
var o;
for(var i=0;i<this.CIObj.Product.length;i++)
{
o=this.CIObj.Product[i];
if(o.href==href)
return o;
}
}
this.Read=function()
{
var o;
for(var i=0;i<this.CIObj.Product.length;i++)
{
o=this.CIObj.Product[i];
document.write("<a href='"+o.href+"' target='_blank'>"+o.title+"</a><br/>");
}
}
}
function CookieObj()
{
this.Product=new Array();
this.Init=function()
{
//生成数组
var match=document.cookie.match(/recent=([^;]+);/i);
var cookieStr=match?match[1]:"";
if(cookieStr.trim()!="")
{
var ps=cookieStr.split("|");
var p;
for(var i=0;i<ps.length;i++)
{
p=ps[i].split(",");
this.Product.push(this.createObj(unescape(p[0]),unescape(p[1]),new Date(unescape(p[2]))));
}
}
}
//重写toString方法
this.toString=function()
{
var Str="",o;
for(var i=0;i<this.Product.length;i++)
{
o=this.Product[i];
Str+=escape(o.href)+","+escape(o.title)+","+escape(o.date)+"|";
}
return Str.trim(/\|$/);
}
//产品对象
this.createObj=function(href,title,date)
{
var o=new Object();
o.href=href;
o.title=title;
if(date)
o.date=date;
else
o.date=new Date();
return o;
}
//按给定的函数排序,如果未指定则按时间desc排序,最新记录显示在最前
this.sort=function(sortFunc)
{
if(sortFunc)
this.Product.sort(sortFunc);
else
this.Product.sort(function(oa,ob){if(oa.date>ob.date) return -1;else return 1;});
}
}//test Code
var CIOp=new CookieOp();
CIOp.AddObj();//添加当前页面
CIOp.Read();//输出,可以把这行放到你要的位置就可以了,如果你改动过实例名称,注意要更改==========================
test1.htm
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
<title>产品测试1</title>
</head>
<body>
<script type="text/javascript" src="recent.js"></script><!--------->
</body>
</html>test2.htm
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
<title>产品测试2</title>
</head>
<body>
<script type="text/javascript" src="recent.js"></script><!--------->
</body>
</html>
这个不错!再加一点,要实现限定最新浏览的3个写入cookie,怎么办呢!!多谢!在线等
再深入一点,要实现限定最新浏览的3个写入cookie,怎么办呢!!多谢!在线等
实现限定最新浏览的3个写入cookie,并且cookie是可以依次更新的...
当有新的写入时,旧的就消失了...
不过你要一次更新COOKIE那可就复杂多了
<script language="javascript" type="text/javascript">
function setCookie(name,value) {
var Days = 1;
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
} function getCookie(name) {
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
return unescape(arr[2]);
else
return "";
}
function setURL(productId){
var productId="<%= id %>";
if(!(getCookie("test").indexOf(productId)>0)){
if(getCookie("test")!="")
setCookie("test",getCookie("test") + "|" + productId);
else
setCookie("test",productId);
}
document.getElementById("txt").innerHTML = "";
document.getElementById("txt").innerHTML = getCookie("test");
}
//document.write(getCookie("test").split("|"));
</script> <%
Dim aa:aa=trim(Request.Cookies("test")&"")
arr = split(aa,"|")
for i=0 to 3
set rs=server.createobject("adodb.recordset")
rs.open "select * from products where bookid="&arr(i),conn,1,3
pic=trim(rs("bookpic"))
cname=rs("bookname")
response.write "<table border=0 cellpadding=5 cellspacing=0>"
response.write "<tr><td>"
response.write "<img src='"&pic&"' alt='"&cname&"' width=80 height=80 border=0 align=absmiddle>"
response.write "</td></tr>"
response.write "</table>"
next
Rs.Close
Set Rs=Nothing以上是完整代码..用js把商品的ID号写入cookie,并用ASP实现读cookie,同时读取数据库调用图片地址显示图片.
读取和写入功能都没问题.
问题就是写入的cookie数量没法限制,因为我要显示最新浏览的最新3个商品.并且是动态更新的...以下是写入的cookie样本:
其中的403 403 391 374 410 是写入的ID号.现在要实现的是怎么限制写入3条,是倒序排列的,要实现最后看的,在最上面显示.通过读写来实现都行,并且还要过滤相同的ID,上面的ID有重复的...
本来我想通过js限制写入来实现,一直没能做到...用ASP读取来实现也行,但是怎么取到最新浏览商品的3个商品,并且过滤重复的ID...对俺是难点..欢迎各位大侠高手帮帮俺...这问题困扰俺很久了...多谢!!!!!!!
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>myvicy view history</title>
<script language="javascript" type="text/javascript">
function setCookie(name, value) //cookies设置
{
var argv = setCookie.arguments;
var argc = setCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : 1;
if(expires!=null&&expires!="")
{
var LargeExpDate = new Date ();
LargeExpDate.setTime(LargeExpDate.getTime() + (expires*1000*3600*24));
}
document.cookie = name + "=" + escape (value)+((expires == null) ? "" : ("; expires=" +LargeExpDate.toGMTString()));
}function getCookie(name) //cookies读取
{
var search = name + "="
if(document.cookie.length > 0)
{
offset = document.cookie.indexOf(search)
if(offset != -1)
{
offset += search.length
end = document.cookie.indexOf(";", offset)
if(end == -1) end = document.cookie.length
return unescape(document.cookie.substring(offset, end))
}
else return ""
}
}
function showHistory(){
url=""
return "<a href="+url+">"+getCookie("pHistory").split("¦").join("</a><br><a href="+url+">")+"</a>"
}
function addHistory(p){
sum=3
cookieStr = getCookie("pHistory")
if(cookieStr!=""){
if(cookieStr.indexOf(p)!=-1)
cookieStr = cookieStr.replace(p+"¦","")
if(cookieStr.split("¦").length>sum)
cookieStr = cookieStr.split("¦").slice(0,sum-1).join("¦")+"¦";
}
cookieStr =p+"¦"+cookieStr
setCookie("pHistory",cookieStr);
document.getElementById("viewHistory").innerHTML = showHistory()
} window.onload=function(){document.getElementById("viewHistory").innerHTML = showHistory()}
</script>
</head>
<body>
<div id="div">
<a href="javascript:void(0);" onclick="addHistory('product1');">商品1 </a>
<a href="javascript:void(0);" onclick="addHistory('product2');">商品2 </a>
<a href="javascript:void(0);" onclick="addHistory('product3');">商品3 </a>
<a href="javascript:void(0);" onclick="addHistory('product4');">商品4 </a>
<div id="viewHistory"> </div>
</div>
</body>
</html>
//改下获取的cookie方式以及设置cookie时此id放最前面
function getCookie(name) {
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
{
//====================
var idc=unescape(arr[2]);
idc=idc.replace(/^\||\|$/g,"");//去掉首尾的|
var ids= idc.split("|");
if(ids.length>2)//======要控制写入多少条改这里
{
//返回最近的2条
return ids[0]+"|"+ids[1];//=======
}
else//没超过3条
return idc;
}
else
return "";
}
function setURL(productId){
var productId="<%= id %>";
if(!(getCookie("test").indexOf(productId)>0)){
if(getCookie("test")!="")
// setCookie("test",getCookie("test") + "|" + productId);////====
setCookie("test",productId+"|"+getCookie("test"));
else
setCookie("test",productId);
}
<script language="javascript" type="text/javascript">
<!--
function setCookie(name,value) {
var Days = 1;
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name) {
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
{
//====================
var idc=unescape(arr[2]);
idc=idc.replace(/^\||\|$/g,"");//去掉首尾的|
var ids= idc.split("|");
if(ids.length>2)//======要控制写入多少条改这里
{
//返回最近的2条
return ids[0]+"|"+ids[1];//=======
}
else//没超过3条
return idc;
}
else
return "";
}function setURL(productId){
var productId="<%= id %>";
if(!(getCookie("test").indexOf(productId)>0)){
if(getCookie("test")!="")
// setCookie("test",getCookie("test") + "|" + productId);////====
setCookie("test",productId+"|"+getCookie("test"));
else
setCookie("test",productId);
}
}
-->
</script> <%
Dim aa:aa=Request.Cookies("test")&""
arr = split(aa,"|")
for i=0 to 3
set rs=server.createobject("adodb.recordset")
Sql="select * from products where bookid="&arr(i)&""
Rs.Open Sql,Conn,1,3
pic=trim(rs("bookpic"))
cname=rs("bookname")
response.write "<table border=0 cellpadding=5 cellspacing=0>"
response.write "<tr><td>"
response.write "<a href='products.asp?id="&arr(i)&"'><img src='"&pic&"' alt='"&cname&"' width=80 height=80 border=0 align=absmiddle></a>"
response.write "</td></tr>"
response.write "</table>"
next
Rs.Close
Set Rs=Nothing
%>以上是修改后的完整代码. 以下是写入的cookie样本:
test403%7C403%7C391%7C374%7C410localhost/1088177062208029923270106654857629923069*
其中的403 403 391 374 410 是写入的ID号. 还有最后一个问题.就是怎么过滤掉cookie写入或者读出的相同的ID号???
如上403只写入一个即可. 谢谢!
//扩展string的原型
String.prototype.trim=function(reg)
{
if(reg)
return this.replace(reg,"");//按给点正则清除不需要的字符
else
return this.replace(/^\s*|\s*$/g,"");//清除两端空白符号
}//试试下面的代码,在setURL时判断是否已经存在id,getCookie还是按照原来的
function getCookie(name) {
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
return unescape(arr[2]);
else
return "";
}
function FindIndex(arr,id)//查找当前id是否在cookie中,用indexOf来查找不太精确,有可能存在111111时,而当前id为111会提示已经存在的错误
{
for(var i=0;i<arr.length;i++)
if(arr[i]==id)
return i;
return -1;//未找到
}
function setURL(productId)
{
var productId="<%=id %>";
var idc=getCookie("test").trim().trim(/^\||\|$/g);//去掉首尾的空白符号和|
if(idc=="")//没有cookie
setCookie("test",productId);
else
{
var ids= idc.split("|");//分离
var OverMax=ids.length>2?true:false;//是否大于存储的数量.........
var Index=FindIndex(ids,idc);
if(Index==-1)//不存在
{
if(OverMax)//超过
ids.splice(ids.length-1,1);//删除最后一个,也就是最早浏览的一个
setCookie("test",productId+"|"+ids.join("|"));//注意使用|连接数组
}
else//存在
{
idc=productId;
ids[Index]="";//清空这个下标的值
//下面连接要存储的id
for(var i=0;i<ids.length;i++)
{
if(ids[i]!="")
idc+="|"+ids[i];
}
setCookie("test",idc);
}
}
}
{
var productId="<%=id %>";
var idc=getCookie("test").trim().trim(/^\||\|$/g);//去掉首尾的空白符号和|
if(idc=="")//没有cookie
setCookie("test",productId);
else
{
var ids= idc.split("|");//分离
var OverMax=ids.length>2?true:false;//是否大于存储的数量.........
var Index=FindIndex(ids,productId);//==============================================
if(Index==-1)//不存在
{
if(OverMax)//超过
ids.splice(ids.length-1,1);//删除最后一个,也就是最早浏览的一个
setCookie("test",productId+"|"+ids.join("|"));//注意使用|连接数组
}
else//存在
{
idc=productId;
ids[Index]="";//清空这个下标的值
//下面连接要存储的id
for(var i=0;i<ids.length;i++)
{
if(ids[i]!="")
idc+="|"+ids[i];
}
setCookie("test",idc);
}
}
}
test.asp
<script>
function setCookie(name,value) {
var Days = 1;
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
//扩展string的原型
String.prototype.trim=function(reg)
{
if(reg)
return this.replace(reg,"");//按给点正则清除不需要的字符
else
return this.replace(/^\s*|\s*$/g,"");//清除两端空白符号
}//试试下面的代码,在setURL时判断是否已经存在id,getCookie还是按照原来的
function getCookie(name) {
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
return unescape(arr[2]);
else
return "";
}
function FindIndex(arr,id)//查找当前id是否在cookie中,用indexOf来查找不太精确,有可能存在111111时,而当前id为111会提示已经存在的错误
{
for(var i=0;i<arr.length;i++)
if(arr[i]==id)
return i;
return -1;//未找到
}
function setURL(productId)
{
var productId="<%=Request.QueryString("id")%>";
if(productId.trim()=="")return;
var idc=getCookie("test").trim().trim(/^\|*|\|*$/g);//去掉首尾的空白符号和|
if(idc=="")//没有cookie
setCookie("test",productId);
else
{
var ids= idc.split("|");//分离
var OverMax=ids.length>2?true:false;//是否大于存储的数量.........
var Index=FindIndex(ids,productId);
if(Index==-1)//不存在
{
if(OverMax)//超过
ids.splice(ids.length-1,1);//删除最后一个,也就是最早浏览的一个
setCookie("test",productId+"|"+ids.join("|"));//注意使用|连接数组
}
else//存在
{
idc=productId;
ids[Index]="";//清空这个下标的值
//下面连接要存储的id
for(var i=0;i<ids.length;i++)
{
if(ids[i]!="")
idc+="|"+ids[i];
}
setCookie("test",idc);
}
}
}
setURL();//=============
</script>
<%
Response.Write "TEST:"&Request.Cookies("test")
%>
<script language="javascript" type="text/javascript">
<!--
function setCookie(name,value) {
var Days = 1;
var exp = new Date();
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name) {
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
{
//====================
var idc=unescape(arr[2]);
idc=idc.replace(/^\||\|$/g,"");//去掉首尾的|
var ids= idc.split("|");
if(ids.length>2)//======要控制写入多少条改这里
{
//返回最近的2条
return ids[0]+"|"+ids[1]+"|"+ids[2];//=======
}
else//没超过3条
return idc;
}
else
return "";
}function setURL(productId){
var productId="<%= id %>";
if(!(getCookie("test").indexOf(productId)>0)){
if(getCookie("test")!="")
// setCookie("test",getCookie("test") + "|" + productId);////====
setCookie("test",productId+"|"+getCookie("test"));
else
setCookie("test",productId);
}
}
-->
</script> <%
Dim aa:aa=Request.Cookies("test")&""
arr = split(aa,"|")
for i=0 to 3
set rs=server.createobject("adodb.recordset")
Sql="select * from products where bookid="&arr(i)&""
Rs.Open Sql,Conn,1,3
pic=trim(rs("bookpic"))
cname=rs("bookname")
'response.write arr(i)
'response.write pic
'response.write cname
response.write "<table border=0 cellpadding=5 cellspacing=0>"
response.write "<tr><td>"
response.write "<a href='products.asp?id="&arr(i)&"'><img src='"&pic&"' alt='"&cname&"' width=56 height=56 border=0 align=absmiddle></a>"
response.write "</td></tr>"
response.write "</table>"
next
Rs.Close
Set Rs=Nothing
%>
经过修改后的完整代码如上.
演示地址如下: http://www.bulux.cn/products.asp?id=357 以下是写入的cookie样本:
test403%7C403%7C391%7C374%7C410localhost/1088177062208029923270106654857629923069*
其中的403 403 391 374 410 是写入的ID号. 现在的问题,就是怎么过滤掉cookie写入或者读出的相同的ID号,就是去掉重复显示的产品???
如上403只写入一个即可. 还有一问题,就是浏览过的产品,只有按F5刷新之后才显示,怎么解决这个问题.(比如进入页面后,能实现自动刷新页面就好了.我找了一些相关语句,如 <meta http-equiv="refresh" content="1">,不过是实现的连续不间断刷新的...怎么才能实现进入页面后自动刷新一次就行.请教各位大侠!) 谢谢!
谢谢各位大虾!结贴了呵!