PDA

View Full Version : gAjax RSS Feeds Displayer v2.0 with read more link



missbeer
07-24-2016, 09:12 AM
1) Script Title: gAjax RSS Feeds Displayer v2.0

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex18/gajaxrssdisplayer.htm

3) Describe problem: i want to add a "read more" link at the end of the description.
With additional code i already made it work that it shows a certain amount of words from the description, so it makes sense to put a read more link there ;)


I tried putting the "read more link" in the function gfeedfetcher(divid, divClass, linktarget). However I don't know how to put the url there as i get confused with all the " ' etc.
Or maybe there is a better solution? I am not so familiar using javascript.


thank you :)





function gfeedfetcher(divid, divClass, linktarget){
this.linktarget=linktarget || "" //link target of RSS entries
this.feedlabels=[] //array holding lables for each RSS feed
this.feedurls=[]
this.feeds=[] //array holding combined RSS feeds' entries from Feed API (result.feed.entries)
this.feedsfetched=0 //number of feeds fetched
this.feedlimit=5
this.showoptions="" //Optional components of RSS entry to show (none by default)
this.outputtemplate="<hr>{title} {label}<br />{date}<br />{description}<br /><br /><a href=' + feeds[i].link + '>Read more...</a>" // output template for each RSS entry
this.regexprules=[] // array to hold regexp rules [regex, replacestr, field_to_apply_to]
this.sortstring="date" //sort by "date" by default
document.write('<div id="'+divid+'" class="'+divClass+'"></div>') //output div to contain RSS entries
this.feedcontainer=document.getElementById(divid)
this.containertag=["li", "<li>"] // [tag to wrap around each rss entry, final tag]
this.onfeedload=function(){}
}

Thank you :)

jscheuer1
07-25-2016, 02:38 PM
OK, first of all, you didn't have to edit the script at all. The change you made/were trying to make, can be made using the feed instance definetemplate function. However, if you want all feeds to have this new template, you can change it in the script as you have already done, just do it correctly (as I'm about to explain).

The real mistake you made was to use feeds[i].link - which in the template has no meaning. What you want is the already available token {url}

So, for example, using the first example feed on the demo page we could do (using it's init, without editing the script at all, impotant additions highlighted):


<script type="text/javascript">

var cssfeed=new gfeedfetcher("example1", "example1class", "")
cssfeed.addFeed("CSS Drive", "http://www.cssdrive.com/index.php/news/rss_2.0/") //Specify "label" plus URL to RSS feed
cssfeed.displayoptions("date snippet") //show the specified additional fields
cssfeed.setentrycontainer("li") //Display each entry as a list (li element)
cssfeed.filterfeed(10, "title") //Show 10 entries, sort by date
cssfeed.definetemplate("<hr>{title} {label}<br />{date}<br />{description}<br /><br /><a href=\"{url}\">Read more...</a>") // output template for this RSS entry
cssfeed.init() //Always call this last

</script>

NOTES: By using the snippet as part of the displayoptions instead of description, we get a shorter version of the description. By defining the definetemplate here, we don't have to change the main script's outputtemplate string. Notice also the escaped (\") internal quotes for the template. It is always necessary in a code string to escape the like quotes which delimit the string. In this case it would be easier to do it this way:


cssfeed.definetemplate('<hr>{title} {label}<br />{date}<br />{description}<br /><br /><a href="{url}">Read more...</a>') // output template for this RSS entry

(using single quotes to delimit the string, allowing for unescaped double quotes inside the string), but either way works.

Here's another, perhaps better example (the snippets here are actually shorter, the css drive descriptions were short already):


<script type="text/javascript">

var socialfeed=new gfeedfetcher("example2", "example2class", "_new")
socialfeed.addFeed("Slashdot", "http://rss.slashdot.org/Slashdot/slashdot") //Specify "label" plus URL to RSS feed
socialfeed.addFeed("Digg", "http://digg.com/rss/index.xml") //Specify "label" plus URL to RSS feed
socialfeed.displayoptions("label datetime snippet") //show the specified additional fields
socialfeed.setentrycontainer("div") //Display each entry as a DIV
socialfeed.filterfeed(6, "label") //Show 6 entries, sort by label
socialfeed.definetemplate('<hr>{title} {label}<br />{date}<br />{description}<br /><br /><a href="{url}">Read more...</a>') // output template for each RSS entry
socialfeed.init() //Always call this last

</script>

missbeer
07-27-2016, 04:26 AM
Great! thank you.

Yes I knew + feeds[i].link + didn't make any sense, but i didn't know {url} so i tired something.

Anyway thanks again!

I have another questions about image resizing but I will continue that in another topic