www.webdeveloper.com
Results 1 to 7 of 7

Thread: Pesky Problems with Recently Viewed Items

  1. #1
    Join Date
    Nov 2009
    Location
    Boston, MA, USA
    Posts
    52

    Pesky Problems with Recently Viewed Items

    Please pardon the dust, as this site is under construction. The URL is http://www.budgetcommercialmailboxes.com/index.html

    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>
    Last edited by ETpro; 11-22-2009 at 03:10 PM.

  2. #2
    Join Date
    Nov 2009
    Location
    Boston, MA, USA
    Posts
    52
    Can anyone help with this? If need be, I can compensate you for time spent. Just let me know in advance what the costs will be.

    Thanks.

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    this should reverse and suppress undefined.
    Code:
    t=n+n.match("undefined")?"":n;
    it's the third to last line...

  4. #4
    Join Date
    Nov 2009
    Location
    Boston, MA, USA
    Posts
    52
    Thanks. That's a start. Replacing
    Code:
    t+=sp+n
    with
    Code:
    t=n+n.match("undefined")?"":n;
    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.

  5. #5
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by ETpro View Post
    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||""));
        }
    }

  6. #6
    Join Date
    Nov 2009
    Location
    Boston, MA, USA
    Posts
    52
    IT WORKS! It works! It WOrks! Woo Hoo.

    Thanks, rnd me. It makes it so much easier to see what's happening with the code indented.

  7. #7
    Join Date
    Jan 2010
    Posts
    1
    (The script embeded in each page to pass values to the above looks like where i have to put this code ? please help i could able to see the products.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles