Two problems with a recently viewed items script. One is an undefined that's getting written with each iteration. How do I get rid of the string of 'undefined'?
The other is that the items write out in the reverse of the order I want them in. I suppose I could just make the containing <div> tags float: right; instead of float: left; and fix this, but it would look odd when only 1 or 2 items have been viewed. I'd like item 1 to display to the far left, then shift to the right as additional items are viewed. Recently viewed items show horizontally at the bottom of the page content, above the footer.
The JS for the recent items is:
Code:
max_recent=3
var expDays=30
var exp=new Date()
exp.setTime(exp.getTime()+(expDays*24*60*60*1000))
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 argv=SetCookie.arguments
var argc=SetCookie.arguments.length
var expires=(argc>2)?argv[2]:null
var path=(argc>3)?argv[3]:null
var domain=(argc>4)?argv[4]:null
var secure=(argc>5)?argv[5]:false
document.cookie=name+"="+escape(value)+((expires==null)?"":("; expires="+expires.toGMTString()))+((path==null)?"":("; path="+path))+((domain==null)?"":("; domain="+domain))+((secure==true)?"; secure":"")
}
function set_recent(v){
var r=new Array()
var i=1,j=1
while((r[i]=GetCookie('etRecentlyViewedCookie'+j))!=null&&r[i]!=""&&j++<=max_recent)
if(r[i]!=v)
i++
if(i>max_recent)
i=max_recent
SetCookie('etRecentlyViewedCookie'+i,v,exp,'/')
j=i
while(++j<=max_recent)
SetCookie('etRecentlyViewedCookie'+j,"",exp,'/')
j=0
if(i==max_recent&&r[i]!=null&&r[i]!="")
j=1
while(--i>0)
SetCookie('etRecentlyViewedCookie'+i,r[i+j],exp,'/')
}
function recent_items(bf,sp,bh){
var i=1,t="",n=""
if((n=GetCookie('etRecentlyViewedCookie'+i))!=null&&n!="")
t+=n
while(++i<=max_recent)
if((n=GetCookie('etRecentlyViewedCookie'+i))!=null&&n!="")
t+=sp+n
if(t!="")
document.write(bf+t+bh)
The script embeded in each page to pass values to the above looks like:
Code:
<script type="text/javascript">recent_items('<h3>Recently Viewed Items!</h3>');set_recent('<div><a href="18-door-4b-horizontal-mailbox-front-loading-a-doors-usps-access-category.html"><img src="/I/yhst-22110758702157_2082_11085" width="90" height="90" border="0" hspace="0" vspace="0" /></a><br /><a href=18-door-4b-horizontal-mailbox-front-loading-a-doors-usps-access-category.html>18 Door (17 Usable) 4B+ Horizontal Mailbox Front Loading A Doors USPS Access</a></div>');</script>
does put the items in correct order, however it doesn't get rid of the string that grows to undefinednedfinedundefined. Also, it appears IE8 can't see the output at all. I see my Recently VIewed Items headline and 3 items in FF and Chrome, but nothing is visible in IE even though it's there in the source code.
however it doesn't get rid of the string that grows to undefinednedfinedundefined. Also, it appears IE8 can't see the output at all. I see my Recently VIewed Items headline and 3 items in FF and Chrome, but nothing is visible in IE even though it's there in the source code.
found a bug.
here's the fixed function:
Code:
function recent_items(bf, sp, bh) {
var i = 0, t = "", n = "";
while (i++ < max_recent) {
if (n = GetCookie("etRecentlyViewedCookie" + i)){
t+= n;
}
}
if (t) {
document.write(bf + t + (bh||""));
}
}
Bookmarks