只显示 一条产品记录.. 且只能在产品详细页面显示..
代码是 根据 http://www.blue1000.com/bkhtml/2006-03/44584.htm 这个帖子代码改写.
JS COOKIE 都不太熟悉.. 找不到问题所在.
求 高人指点..
------------产品详细页面代码--------- <script language="javascript" type="text/javascript"> //cookie的相关函数 function getCookieVal(offset) { var endstr = document.cookie.indexOf(";", offset); if (endstr == -1) endstr = document.cookie.length; return unescape(document.cookie.substring(offset, endstr)); } 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 null; } function setCookie(name, value) { var exp = new Date(); exp.setTime(exp.getTime() + 3600000000); document.cookie = name + "=" + value + "; expires=" + exp.toGMTString(); } $(function () { var pro_url = document.URL; //页面地址 var pro_name = $(".show_right h1").text(); //产品名称 var pro_img = $("#zoom1 img")[0].src; //图片路径 var pro_online = $(".proinquire").attr("href"); //询盘路径 wlink = pro_url + "+" + pro_name + "+" + pro_img + "+" + pro_online +"|"; old_info = getCookie("history_info"); var insert = true; //////////////////////// if (old_info == null) //判断cookie是否为空 { insert = true; } else { var old_link = old_info.split("|"); for (var j = 0; j <= 5; j++) { if (old_link[j].indexOf(pro_url) != -1) 打开页面这里报错 'undefined' 为空或不是对象 insert = false; } } ///////////////////////////// if (insert) //如果符合条件则重新写入数据 { wlink += getCookie("history_info"); setCookie("history_info", wlink); history_show().reload(); } }) </script> -------------其他页面显示代码--------------- <script> //cookie的相关函数 function getCookieVal(offset) { var endstr = document.cookie.indexOf(";", offset); if (endstr == -1) endstr = document.cookie.length; return unescape(document.cookie.substring(offset, endstr)); } 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 null; } function history_show() { var history_info=getCookie("history_info"); var content=""; if(history_info!=null) { history_arg=history_info.split("|"); var i; for(i=0;i<=5;i++) { if(history_arg[i]!="null") { var wlink = history_arg[i].split("+"); 此处报错.'history_arg[...]' 为空或不是对象 content = "最近浏览记录"; // content+=(wlink[0]); content += ("<li><em><a href='" + wlink[0] + "'><img src='" + wlink[2] + "' width='50' height='39'></a></em>"); content += ("<div><strong><a href='" + wlink[0] + "'>" + wlink[1] + "</a></strong><span>[<a href='" + wlink[3] + "'>在线询盘</a>]</span></div>") } document.getElementById("history").innerHTML=content; } } else {document.getElementById("history").innerHTML="对不起,您没有任何浏览纪录";} } </script>
<script language=javascript> |
002 |
|
003 |
//获得coolie 的值 |
004 |
|
005 |
|
006 |
|
007 |
function cookie(name){ |
008 |
|
009 |
var cookieArray=document.cookie.split( "; " ); //得到分割的cookie名值对 |
010 |
|
011 |
var cookie= new Object(); |
012 |
|
013 |
for (var i= 0 ;i<cookieArray.length;i++){ |
014 |
|
015 |
var arr=cookieArray[i].split( "=" ); //将名和值分开 |
016 |
|
017 |
if (arr[ 0 ]==name) return unescape(arr[ 1 ]); //如果是指定的cookie,则返回它的值 |
018 |
|
019 |
} |
020 |
|
021 |
return "" ; |
022 |
|
023 |
} |
024 |
|
025 |
|
026 |
|
027 |
function delCookie(name) //删除cookie |
028 |
|
029 |
{ |
030 |
|
031 |
document.cookie = name+ "=;expires=" +( new Date( 0 )).toGMTString(); |
032 |
|
033 |
} |
034 |
|
035 |
|
036 |
|
037 |
function getCookie(objName){ //获取指定名称的cookie的值 |
038 |
|
039 |
var arrStr = document.cookie.split( "; " ); |
040 |
|
041 |
for (var i = 0 ;i < arrStr.length;i ++){ |
042 |
|
043 |
var temp = arrStr[i].split( "=" ); |
044 |
|
045 |
if (temp[ 0 ] == objName) return unescape(temp[ 1 ]); |
046 |
|
047 |
} |
048 |
|
049 |
} |
050 |
|
051 |
|
052 |
|
053 |
function addCookie(objName,objValue,objHours){ //添加cookie |
054 |
|
055 |
var str = objName + "=" + escape(objValue); |
056 |
|
057 |
if (objHours > 0 ){ //为时不设定过期时间,浏览器关闭时cookie自动消失 |
058 |
|
059 |
var date = new Date(); |
060 |
|
061 |
var ms = objHours* 3600 * 1000 ; |
062 |
|
063 |
date.setTime(date.getTime() + ms); |
064 |
|
065 |
str += "; expires=" + date.toGMTString(); |
066 |
|
067 |
} |
068 |
|
069 |
document.cookie = str; |
070 |
|
071 |
} |
072 |
|
073 |
|
074 |
|
075 |
function SetCookie(name,value) //两个参数,一个是cookie的名子,一个是值 |
076 |
|
077 |
{ |
078 |
|
079 |
var Days = 30 ; //此 cookie 将被保存 30 天 |
080 |
|
081 |
var exp = new Date(); //new Date("December 31, 9998"); |
082 |
|
083 |
exp.setTime(exp.getTime() + Days* 24 * 60 * 60 * 1000 ); |
084 |
|
085 |
document.cookie = name + "=" + escape (value) + ";expires=" + exp.toGMTString(); |
086 |
|
087 |
} |
088 |
|
089 |
function getCookie(name) //取cookies函数 |
090 |
|
091 |
{ |
092 |
|
093 |
var arr = document.cookie.match( new RegExp( "(^| )" +name+ "=([^;]*)(;|$)" )); |
094 |
|
095 |
if (arr != null ) return unescape(arr[ 2 ]); return null ; |
096 |
|
097 |
|
098 |
|
099 |
} |
100 |
|
101 |
function delCookie(name) //删除cookie |
102 |
|
103 |
{ |
104 |
|
105 |
var exp = new Date(); |
106 |
|
107 |
exp.setTime(exp.getTime() - 1 ); |
108 |
|
109 |
var cval=getCookie(name); |
110 |
|
111 |
if (cval!= null ) document.cookie= name + "=" +cval+ ";expires=" +exp.toGMTString(); |
112 |
|
113 |
} |
114 |
|
115 |
</script> |
楼主,先把你的JS代码贴好了