Get Blogger Post - Rss Feed in our website using javascript

Task: Get Blogger post - Rss Feed in our website using javascript.

Description: It shows 2 post and summery of post without any html tag like without any image. If you want to show all content then just use content as it is without any operation on it.


­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<div id="blog-feed"></div>
<script type="text/javascript">
  google.load("feeds", "1");

  var feedContainer=document.getElementById("blog-feed");
  var feedURL="https://hemantrautela.blogspot.com/feeds/posts/default?alt=rss";
  var postsReturned=2;
  var rssoutput="<div style='background-color:green'>";
  var stringWithHTML="";

  function rssFeedSetup(){
    var pullFeed=new google.feeds.Feed(feedURL)
    pullFeed.setNumEntries(postsReturned)
    pullFeed.load(showFeed)
  }

  function showFeed(result){
    if (!result.error){
      var thefeeds=result.feed.entries;
      for (var i=0; i<thefeeds.length; i++)
      {
       stringWithHTML = thefeeds[i].content.replace(/<\/?[a-z][a-z0-9]*[^>]*>/ig, ""); // remove html tag
      if(stringWithHTML.length>200) // just a summery of post
       {
       stringWithHTML= stringWithHTML.substring(0,200);
       var n = stringWithHTML.lastIndexOf(" ");
       stringWithHTML= stringWithHTML.substring(0,n)+"...";
       }
      rssoutput+="<div><a target='_blank' style='color:#ddd; font-size:16px;' href='" + thefeeds[i].link + "'rel='nofollow'>" + thefeeds[i].title + "</a><br/>" + stringWithHTML +"</div><hr/>"
      }
      rssoutput+="</div>";
      feedContainer.innerHTML=rssoutput;
    }
    else
      {
       rssoutput ="There was an error loading the blog posts.";
       feedContainer.innerHTML=rssoutput;
      }
    }
    window.onload=function(){
    rssFeedSetup()
  }

</script>­­­

No comments:

Post a Comment