0

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();
});
1
  • what do you mean with recursive code? Commented Sep 19, 2016 at 6:47

1 Answer 1

1

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");
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.