Results 1 to 4 of 4

Thread: Cookie based recently viewed pages script

  1. #1
    Join Date
    Oct 2012
    Posts
    157
    Thanks
    21
    Thanked 1 Time in 1 Post

    Default Cookie based recently viewed pages script

    How can I make this script to show the title of an ad instead of showing the title of a page? Right now it simply grabs whatever is between <title></title>.

    An ad's title is echoed like this <?php echo $adtitle; ?>. Obviously it's in php, but I am wondering if you, guys, somehow can integrate it into the script so that it would show an ad's title instead of the stuff between <title></title>.

    Code:
    <script type="text/javascript"><!--
    // Obtained from http://www.willmaster.com/
    //
    // Specify maximum number of history links to keep,
    //    minimum 1.
    var MaximumNumberOfLinks = 15;
    
    // Specify cookie name.
    var CookieName = "HistoryLinks";
    
    // Specify number of days cookie is to remain on visitor's
    //    computer. (Use 0 to delete cookie when browser closed.)
    var DaysToLive = 366;
    
    // No other customizations required.
    var HistoryLink = new Array();
    var HistoryTitle = new Array();
    var CurrentPage = new String();
    var HistoryContent = new String();
    DaysToLive = parseInt(DaysToLive);
    MaximumNumberOfLinks = parseInt(MaximumNumberOfLinks);
    if( MaximumNumberOfLinks < 1 ) { MaximumNumberOfLinks = 10; }
    
    function GetCookie() {
    var cookiecontent = '';
    if(document.cookie.length > 0) {
       var cookiename = CookieName + '=';
       var cookiebegin = document.cookie.indexOf(cookiename);
       if(cookiebegin > -1) {
          cookiebegin += cookiename.length;
          var cookieend = document.cookie.indexOf(";",cookiebegin);
          if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
          cookiecontent = document.cookie.substr(cookiebegin,cookieend);
          }
       }
    if( cookiecontent.length < 3 ) { return; }
    cookiecontent = unescape(cookiecontent);
    var historyList = cookiecontent.split('&');
    for( var i = 0; i < historyList.length; i++ ) {
       var link = historyList[i].split('=',2);
       HistoryLink.push(link[0]);
       HistoryTitle.push(link[1]);
       var temparray = link[0].split('~amp;');
       link[0] = temparray.join('&');
       temparray = link[1].split('~amp;');
       link[1] = temparray.join('&');
       HistoryContent += '<'+'p>'+'<'+'a href="'+link[0]+'">'+link[1]+'<'+'/'+'a>'+'<'+'/'+'p>';
       }
    }
    
    function PutCookie() {
    if( HistoryLink.length < 1 ) { return; }
    var len = HistoryLink.length;
    while( HistoryLink.length > MaximumNumberOfLinks ) {
       HistoryTitle.shift();
       HistoryLink.shift();
       }
    var pairs = new Array();
    for( var i = 0; i < HistoryLink.length; i++ ) { pairs.push(HistoryLink[i]+'='+HistoryTitle[i]); }
    var value = pairs.join('&');
    var exp = new String();
    if(DaysToLive > 0) {
       var now = new Date();
       now.setTime( now.getTime() + (DaysToLive * 24 * 60 * 60 * 1000) );
       exp = '; expires=' + now.toGMTString();
       }
    document.cookie = CookieName + "=" + escape(value) + '; path=/' + exp;
    }
    
    function RecordCurrentPage() {
    var link = document.URL;
    var title = document.title.length > 1 ? document.title : 'Untitled';
    CurrentPage = '<'+'p>'+title+'<'+'/'+'p>';
    var temparray = link.split('&');
    link = temparray.join('~amp;');
    var temparray = title.split('&');
    title = temparray.join('~amp;');
    HistoryLink.push(link);
    HistoryTitle.push(title);
    }
    
    GetCookie();
    RecordCurrentPage();
    PutCookie();
    //--></script>
    Code:
    <script type="text/javascript"><!--
    if(HistoryContent.length) {
    
    document.write('<'+'div ');
    document.write(' id="visitorhistory" ');
    document.write(' style="border-style:solid; border-width:1px; padding:0 5px 10px 20px;">');
    
    document.write('<'+'h3>You were here:<'+'/h3>');
    
    document.write(HistoryContent);
    
    document.write('<'+'h3>You are here:<'+'/h3>');
    
    document.write(CurrentPage);
    
    document.write('<'+'/div>');
    
    }
    //--></script>
    Last edited by qwikad.com; 07-21-2014 at 11:23 PM.

  2. #2
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,382
    Thanks
    31
    Thanked 148 Times in 142 Posts
    Blog Entries
    34

    Default

    I don't know exactly what you are trying to accomplish, so I may be missing the point.
    What the script shows (as far as the title is concerned) is governed by CurrentPage = .... You can put anything you want there, for instance an image:
    Code:
    CurrentPage = '<'+'p>'+'<img src="http://www.boomerangtv.nl/sites/www.boomerangtv.nl/files/imagecache/character_image/characters/flintstones-barney1.gif">'+'<'+'/'+'p>';
    But since you mention PHP, why not use it for what you want? Remember that your cookies will all be gone in a given browser as soon as its history is cleared.

  3. #3
    Join Date
    Oct 2012
    Posts
    157
    Thanks
    21
    Thanked 1 Time in 1 Post

    Default

    Yeah, but the HistoryTitle isn't governed by that. I want this to grab and remember the recently visited ads' titles. That link[1] is what the title is between <title></title> I want it to display $adtitle instead.

    Code:
    if( cookiecontent.length < 3 ) { return; }
    cookiecontent = unescape(cookiecontent);
    var historyList = cookiecontent.split('&');
    for( var i = 0; i < historyList.length; i++ ) {
       var link = historyList[i].split('=',2);
       HistoryLink.push(link[0]);
       HistoryTitle.push(link[1]);
       var temparray = link[0].split('~amp;');
       link[0] = temparray.join('&');
       temparray = link[1].split('~amp;');
       link[1] = temparray.join('&');
       HistoryContent += '<'+'p>'+'<'+'a href="'+link[0]+'">'+link[1]+'<'+'/'+'a>'+'<'+'/'+'p>';
       }
    }

  4. #4
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,382
    Thanks
    31
    Thanked 148 Times in 142 Posts
    Blog Entries
    34

    Default

    I wouldn't know how to do that in this script (without PHP). There are some PHP-experts on these forums. They may have a solution.

Similar Threads

  1. Lightbox for Facebook Likebox - cookie based??
    By goldenimage in forum JavaScript
    Replies: 1
    Last Post: 12-06-2011, 09:06 PM
  2. Show/hide div based on cookie value
    By bigalo in forum JavaScript
    Replies: 2
    Last Post: 11-12-2009, 05:24 PM
  3. Javascript stylesheet swap based on cookie
    By kristiandavid in forum JavaScript
    Replies: 2
    Last Post: 10-21-2009, 05:41 PM
  4. Cookie only serving pages on root directory?!
    By mrmozer in forum JavaScript
    Replies: 10
    Last Post: 04-19-2009, 02:50 AM
  5. a script that displays a div if the page has been viewed or not...
    By iphopper in forum Looking for such a script or service
    Replies: 0
    Last Post: 04-10-2009, 11:11 PM

Bookmarks

Posting Permissions

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