Is there a way to avoid repeating the same code for different feeds? I have 8 feeds that I would like to use and currently, I am just incrementing variable names and feed URLs.
<script type="text/javascript>
function showFeed(data, contentId){
var element = document.getElementById(contentId);
if(data.status == 'ok'){
var feedLength = data.items.length;
var output = '';
for(var i=0;i<1;++i){
output += '<p><a href="' +
data.items[i].link + '" target="_blank" >' +
data.items[i].title + '</a>';
}
element.innerHTML = output;
}
}
// Loop through multiple feeds
var feedsInfo = [
{id: 'cpa', url: 'http://feedurlplaceholder1'},
{id: 'fs', url: 'http://feedurlplaceholder2'},
{id: 'wealth', url: 'http://feedurlplaceholder3'}
];
feedsInfo.forEach(function(feed, index) {
var scriptElement = document.createElement('script');
scriptElement.src = "http://rss2json.com/api.json?callback=showFeed"+ (index+1) +"&rss_url="+ encodeURI(feed.url);
document.body.appendChild(scriptElement);
});
</script>