Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Issue with an RSS feed parsed by JS showing old dates

    The RSS feed has no dates on it at all. However once this code runs, it will still display old dates/events anyways. I have been working on this for awhile with no avail.

    The actual RSS feed isn't view able to those outside the network but here is the JS.

    I want ONLY future dates. As of now a display_all gives me dates back from January and then only 1 future date.


    Code:
    (function($){
    
    var source = {
    
      init: function(o){
    
    
        /*-----------------   Configuration   ------------------------*/
    
        var filter_by   = o.filter_by;
        var filter_by_2 = o.filter_by_2; 
        var count       = o.count;
        var feed_url    = o.feed_url;
        var link_url    = o.link_url
        var display_all = o.display_all; //Set to 'true' to display all calendar entries without filters
    
        /*----------------- end Configuration  ------------------------*/
    
        /*----------------- No need to edit below this line  ------------------------*/
    
        //Preloader
        $('div#calendar').prepend('<div id="loading"></div>');
    
        //Months to be used
        var month_names = new Array ();
        month_names[month_names.length] = "Jan";
        month_names[month_names.length] = "Feb";
        month_names[month_names.length] = "Mar";
        month_names[month_names.length] = "Apr";
        month_names[month_names.length] = "May";
        month_names[month_names.length] = "Jun";
        month_names[month_names.length] = "Jul";
        month_names[month_names.length] = "Aug";
        month_names[month_names.length] = "Sep";
        month_names[month_names.length] = "Oct";
        month_names[month_names.length] = "Nov";
        month_names[month_names.length] = "Dec";
    
        //days of the week
        var day_of_week = new Array ();
        day_of_week['1'] = "Monday";
        day_of_week['2'] = "Tuesday";
        day_of_week['3'] = "Wednesday";
        day_of_week['4'] = "Thursday";
        day_of_week['5'] = "Friday";
        day_of_week['6'] = "Saturday";
        day_of_week['7'] = "Sunday";
    
        //Fix timezone issue
        Date.prototype.addHours= function(h){
            this.setHours(this.getHours()+h);
            return this;
        }
    
        //function to format time
        function formatTime(date) {
            //Change this "-5" to "-4" when the timezone changes
            var d  = new Date(date).addHours(-5);
            var hh = d.getUTCHours();
            var m  = d.getMinutes();
            var dd = "am";
            var h  = hh;
    
            //Day of the week
            var day = day_of_week[d.getDay(date)];
    
            //hours to am pm
            if(hh > 12){
                hh = hh-12;
                dd = "pm"
            } 
    
            //minutes
            m = m<10?"0"+m:m;
    
            return(day + ' at ' + hh + ':' + m + dd );
        }
    
        new Date(0).toString()
        //function to convert plain text to links
        function replaceURLWithHTMLLinks(text) {
            var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
            return text.replace(exp,"<a href='$1'>$1</a>"); 
        }
    
        //Load the feed
          google.load("feeds", "1");
    
        //For espacing html characters
          // String.prototype.unescapeHtml = function () {
          //   var temp       = document.createElement("div");
          //   temp.innerHTML = this;
          //   var result     = temp.childNodes[0].nodeValue;
          //   temp.removeChild(temp.firstChild);
          //   return result;}
    
        // Our callback function, for when a feed is loaded.
        function feedLoaded(result) {
    
          if (!result.error) {
    
            // Grab the container we will put the results into
            var content = document.getElementById("calendar");
            var html    = '';
    
            // Get current height of container div then set it manually (for animation later)
            var containerHeight = $(content).outerHeight();
            $(content).css({height: containerHeight});
    
            //Function that removes date from begining of title string
            function removeDateTitle(strValue){
                var string = strValue.substr(16);
                var string = $.trim(string);
                return string;
            };
    
            function removeDateDesc(strValue){
                var string = strValue.substr(25);
                var string = $.trim(string);
                return string;
            };
    
            // Check out the result object for a list of properties returned in each entry.
            // http://code.google.com/apis/ajaxfeeds/documentation/reference.html#JSON
    
            for (var i = 0; i < result.feed.entries.length; i++) {
    
    
              var entry           = result.feed.entries[i];
            console.log(result.feed.entries.length);
              //RegEx queries for 'match' method
              var filter_by_REG   = RegExp(filter_by, 'i');
              var filter_by_2_REG = RegExp(filter_by_2, 'i');
    
              var cond1           = result.feed.entries[i].title.match(filter_by_REG);
              var cond2           = result.feed.entries[i].title.match(filter_by_2_REG);
              var today           = new Date;
              var pubDate         = new Date(entry.publishedDate);
    
            console.log("pubdate = " + pubDate + "todays" + today);
    
    
    
              //Display All items
              if(display_all){
    
                cond1 = true; cond2 = true;
    
              }
    
    
    
              //Query entries for matches and add them to "entry" array for display      
              if(cond1 || cond2 ){
    
                var pubDate     = new Date(entry.publishedDate);
    
                var pubMonth    = month_names[pubDate.getMonth()];
                var pubdateNum  = pubDate.getDate();
                var href        = 'http://calendargoeshere.aspx';//entry.link;
                var title       = removeDateTitle(entry.title);
                var location    = entry.categories[0];
                    if(location != undefined){
                        location    = location.replace(/[+]/g ," ");
                    }
    
    
                var time        = formatTime( entry.publishedDate );
                var desc = entry.content; 
                    desc = removeDateDesc(desc);
                    desc_short = desc.split(" ").splice(0, 25).join(" ");
                    desc_short += "...";
    
    
                var fb_url ='<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.facebook.com/f&action=recommend" scrolling="no" frameborder="0" class="fb_like_btn"></iframe>';
    
                // Output
                html += '<div class="calentry' + ' tooltip_' + i + '">' + '<div class="calcon">' + '<div class="caltop">' + pubMonth + '</div>' + '<div class="calbottom">' + 
                pubdateNum + '</div>' + '</div>' + '<div class="description"><h3 class="calhead"><a href="' + href + '" target="_blank">' + title + '</a></h3>' + 
                '<div class=callocation>' + time + ' in ' + location + '</div>' + '<p>' + desc_short + '<br />' + 
                '</p></div><div class="clear"></div>' + '</div>';
    
                //QtipContent
                html += '<div style="display: none;">' + 
                '<b>Full Description:</b><br />' + replaceURLWithHTMLLinks(desc) + '<br />' +
                '<p><b>Time: </b>' + time + '<br />' +
                '<b>Location: </b>' + location +
                '</p>' + fb_url +'</div>';
                //throw new Error(html);
                //update content
                content.innerHTML = html;
    
              }
    
    
    
    
            }//end loop
    
    
            //Display message when no feed items found
            if(html == ''){
              html += '<p class="cal_noEvents">No events currently scheduled.</p>';
              content.innerHTML = html;
            }
    
            //Hide PreLoader
            $('#loading').fadeOut('slow');
    
            //Animate loaded content
            $(content).wrapInner('<div class="calendarInner" />'); //wrap with inner div
            var contentHeight = $('.calendarInner').outerHeight();// Get the height of the content div.
            contentHeight = contentHeight;
            $(content).animate({height:contentHeight}, 1000); // Animate the height 
            $('.calendarInner').css('width','100%'); //Fix border line issue after animation
    
            //Qtip
    
            var cal_items 
            $('.calentry').each(function() {
                var thisEntry = $(this);
    
                thisEntry.qtip(
                {
                    content: {
                        text: thisEntry.next('div:hidden'),
                        title: {
                            text: thisEntry.find('h3').text(),
                            button: true
                        }
                    },
                    position: {
                        my: 'center', // ...at the center of the viewport
                        at: 'center',
                        target: $(window)
                    },
                    show: {
                        event: 'click', // Show it on click...
                        solo: true, // ...and hide all other tooltips...
                        modal: true // ...and make it modal
                    },
                    hide: false,
                    style: {
                        classes: 'ui-tooltip-light ui-tooltip-rounded',
                        width: 500
                    }
                });   
            }).bind('click', function(event){ event.preventDefault(); return false; }); 
            //end qtip 
    
    
          }
    
        }
        function OnLoad() {
          // Create a feed instance that will grab the feed.
          var feed = new google.feeds.Feed(feed_url);
          feed.setNumEntries(count);
    
          // Calling load sends the request off.  It requires a callback function.
          feed.load(feedLoaded);
    
        }
    
        google.setOnLoadCallback(OnLoad);
    
    
      } //end init
    
    };
    
    $.fn.extend({ 
    
        //pass the options variable to the function
        calendar: function(options) {
    
    
            //Set the default values, use comma to separate the settings, example:
            var defaults = {
                filter_by   : false,
                filter_by_2 : false,
                count       : 5,
                display_all : false,
                feed_url    : 'noFeed',
                link_url    : 'http://calendarrssfeedhere.aspx'
            }
    
            var options =  $.extend(defaults, options);
    
                var o = options;
    
                //Call the init method (get the ball rollin) -- passing options 'o'
                    source.init(o);
        }
    });
    
    })(jQuery);

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    This is *WRONG*:
    Code:
        var day_of_week = new Array ();
        day_of_week['1'] = "Monday";
        ... etc ...
    
        var day = day_of_week[d.getDay(date)];
    getDay() returns 0 for Sunday, 1 for Monday, etc.

    It's also wrong to use day_of_week['1'] with quotes around the index.

    KEEP IT SIMPLE!
    Code:
    var month_names = ["Jan","Feb","Mar","Apr","May","Jun",
                       "Jul","Aug","Sep","Oct","Nov","Dec"];
    var day_of_week = ["Sunday","Monday","Tuesday",
                       "Wednesday","Thursday","Friday","Saturday"];
    Okay...now I'll look at the rest of it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    How can you say
    The RSS feed has no dates on it at all.
    and yet quite clearly the code is expecting a pubDate in the RSS feed, as is evidenced by
    Code:
                var pubDate     = new Date(entry.publishedDate);
    and if you don't want all entries with any date, then where is the code to limit same?

    Maybe you need to show us a sample of the XML in the RSS feed.

    You can obscure private content, but at least show us the tags used.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

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