user3382310
user3382310

Reputation: 23

Can i make a function in javascript to bypass recursive code?

I am not quiet good with JS. I was working on Rss parser. This is the Js i am using. So for every feed link, I have to repeat the whole code. ( the part from limit: 140 to )

can it be made smaller and less repetitive? so that everytime i add any new link with new ID, i dont have to copy all the below details with it.

function load_rss(){

 $("#rss-container").rss("http://www.hindufaqs.com/feed/", {
    limit: 140,
    ssl: true,
    effect: 'show',
    dateFormat: 'MMMM DD, YYYY',
    entryTemplate: 
    "<a class='entry_link' href='{url}'>\
      <div class='entry'>\
        <div class='entry_date'>\
          Submitted: {date}\
          <span class='entry_by'>by: {author}</span>\
          \
        </div>\
        <div class='entry_img'>\
          {teaserImage}\
        </div>\
        <div class='entry_title'>\
          {title}\
        </div>\
        <div class='entry_details'>{shortBody}</div>\
      </div>\
    </a>",
  });
}

$(function(){
  load_rss();
});

Upvotes: 0

Views: 61

Answers (1)

Yoshi
Yoshi

Reputation: 54649

Parameterize the load_rss.

function load_rss(target, url){
  $(target).rss(url, {
    limit: 140,
    ssl: true,
    effect: 'show',
    dateFormat: 'MMMM DD, YYYY',
    entryTemplate: 
    "<a class='entry_link' href='{url}'>\
      <div class='entry'>\
        <div class='entry_date'>\
          Submitted: {date}\
          <span class='entry_by'>by: {author}</span>\
          \
        </div>\
        <div class='entry_img'>\
          {teaserImage}\
        </div>\
        <div class='entry_title'>\
          {title}\
        </div>\
        <div class='entry_details'>{shortBody}</div>\
      </div>\
    </a>"
  });
}

Then you can do this:

$(function(){
  load_rss("#rss-container", "http://www.hindufaqs.com/feed/");
  load_rss("#rss-container2", "different url");
  load_rss("#rss-container3", "and another");
});

Upvotes: 1

Related Questions