i want a script (it doesn't really matter what kind; but i would like javascript) than can check from a RSS feed, if the newest post, is posted to a max. of 1 day ago. So, if it's more than 1 day ago posted, it should do nothing, if it is between 0-1 days ago posted, it should show 'New' in text.

Can anybody something like this? Can you give me a script? Thanks.

  • Is the RSS feed on the same domain as the page? Otherwise Javascript is not an option for reading it... Guffa over 9 years ago
  • @guffa - yes it is on the same domain TutorialPoint over 9 years ago

1 answer

Mottie 1134

Actually you can read RSS feeds on any domain using Google's Feed API

I posted a demo of a feed where posts older than a set number of days are hidden (faded out in the demo)

Here is the code


<div id="feedContainer"></div>

Scripts loaded (the feed plugin can be put into a separate file, it's included here & in the demo, so I don't have to host the script):

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
 *  Plugin which uses the Google AJAX Feed API for creating feed content
 *  @author:  M. Alsup (malsup at gmail dot com)
 *  @version: 1.0.2 (5/11/2007)
 *  Documentation and examples at: http://www.malsup.com/jquery/gfeed/
 *  Free beer and free speech. Enjoy!
if (!window.google){
 alert('You must include the Google AJAX Feed API script');
if (!google.feeds) google.load("feeds", "1");
$.fn.gFeed = function(options){
 var opts = jQuery.extend({
  target: this,
  max: 5 /* max number of items per feed */
 }, options || {});
 var g = new google.feeds.FeedControl();
 this.each(function() {
  var url = this.href || opts.url;
  var title = opts.title || this.title || $(this).text();
  g.addFeed(url, title);
  g.draw(this, opts.tabs ? { drawMode: google.feeds.FeedControl.DRAW_MODE_TABBED } : null );
 return this;

Working Script

<script type="text/javascript">
 var feedTitle = 'Ctrl+Alt+Del';
 var feedURL = 'http://feeds.feedburner.com/cad-comic?format=xml';
 var feedNumber = 5;
 var daysOld = 2;

 // Get and Set date of # daysOld
 var daysAgo=new Date();
 daysAgo.setDate(daysAgo.getDate() - daysOld);

 // load the Feed
  .gFeed({ url:feedURL, 'xkcd':feedTitle, 'max':feedNumber });

 // Sadly there is no feed callback, so we just have to wait 1 second
   var pubDate = $(this).find('.gf-relativePublishedDate').text();
   if (new Date(pubDate) > daysAgo || pubDate.match('minute|hour')) {
Answered over 9 years ago by Mottie
  • Thanks for your code, but it isnt exactly what i want. I dont want to display the feed, i just want to show a little piece of text like 'new post' if the newest post isnt older than 1 day. TutorialPoint over 9 years ago