"New to you" javascript functionality

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Wolfman
    New Member
    • Apr 2007
    • 21

    "New to you" javascript functionality

    Hi all

    Upon seeking to differentiate new content from old in an HTML list based upon when each user last visited, I began researching "new to you" functionality. It's used in several forums like Yabb to place a small graphic next to each newly-posted or updated link in the list, monitored by a cookie, then disappears after the link is clicked to indicate the topic has been read.

    However, surprisingly there seems to be a dearth of demand for a script to do the same thing on plain old web pages. After extensive searching, only a single script seems to appear again and again for this purpose. Although it's concisely written, it doesn't have the necessary functionality as it is placed on the content page itself, not tied to the link that selects that page. So, for example, if three items in the list are new, that particular script tells them so, but then all three disappear when just one link is selected, because the script is actually telling the user that they have now visited the index page itself, not the particular article.

    What I'm looking for is a way to tie the cookie to the HREF link, so it won't disappear until the link itself is clicked.

    Wolfman

    Here's that code. Notice that the BODY code specifies the date, which is compared by the script to the cookie's last recorded visit. So the desired change would be "how do I compare the date to the link's last recorded click?"

    Code:
     
    now = new Date
    expireDate = new Date
    expireDate.setMonth(expireDate.getMonth()+6)
    lastVisit = new Date(cookieVal("pageVisit"))
    document.cookie = "pageVisit="+now+";expires=" + expireDate.toGMTString()
    function cookieVal(cookieName) {
      thisCookie = document.cookie.split("; ")
        for (i=0; i<thisCookie.length; i++) {
          if (cookieName == thisCookie[i].split("=")[0]) {
            return thisCookie[i].split("=")[1]
          }
        }
      return "1 January 1970"
    }
    function newCheck(yyy,mm,dd) {
      lastChgd = new Date(yyy,mm-1,dd)
      if (lastChgd.getTime() > lastVisit.getTime()) {
        document.write("<img src='/img/new-to-you/new.gif' alt='new'>")
      }
    }
    Code:
    <script type="text/javascript" language="JavaScript"> newCheck(2005,2,3)</script>
    Last edited by Wolfman; Jan 11 '09, 12:13 AM. Reason: added code
  • Wolfman
    New Member
    • Apr 2007
    • 21

    #2
    I was thinking that the script could be split into two components to achieve the same goal:

    1. A script on the content page sets the cookie of last visit
    2. A script on the index page checks the cookie and displays the graphic.

    The trick is that each content page would have to have a unique cookie so each index page script would know which one it is tracking. Ideas?
    Last edited by Wolfman; Jan 11 '09, 09:18 PM. Reason: spelling correction

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      You could use the link onclick to delete/expire cookies.

      Comment

      • Wolfman
        New Member
        • Apr 2007
        • 21

        #4
        OK. Now, as to designating the target cookie, PageVisit seems to be the variable that specifies this page. How can we make it look for that page?

        Code:
        lastVisit = new Date(cookieVal("pageVisit"))document.cookie = "pageVisit="+now+";expires=" + expireDate.toGMTString()
        Last edited by Dormilich; Jan 15 '09, 07:00 AM. Reason: added [code] tags

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          You can reuse the same cookie and use name-value pairs to differentiate between different values - see JavaScript - Cookies

          Comment

          Working...