#Javascript Rss Reader#
Takes a rss feed and prints html (defined by a template) for every post.
#Javascript Rss Reader#
Takes a rss feed and prints html (defined by a template) for every post.
| <!DOCTYPE html> | |
| <div id=blog></div> | |
| <script> | |
| //function to make "XMLHttpRequest" object chainable | |
| (function(z,a,b){ | |
| for(a in b=z.prototype)(function(c,d){b[a]=function(){d=c.apply(this,arguments);return d===undefined?this:d}})(b[a]); | |
| })(XMLHttpRequest); | |
| //end | |
| function printRss(file,template){ | |
| var | |
| //counter: | |
| i, | |
| //the string that will be returned | |
| txt = "", | |
| //get all "entry" elements from a rss feed | |
| x = (new XMLHttpRequest) | |
| .open("GET",file,false) | |
| .send() | |
| .responseXML | |
| .documentElement | |
| .getElementsByTagName("entry"), | |
| //a templating function that outputs the content of the rss xml tags when it encounters double curly braces {{}} | |
| //example: "{{content}}" will output the content of the "<content>" xml tag of the rss feed | |
| tmpl = function(){ | |
| return template.replace(/\{\{([^{}]+)}}/g,function(b,c){ | |
| return x[i].getElementsByTagName(c)[0].firstChild.nodeValue | |
| }) | |
| }; | |
| for(i=x.length;i--;){ | |
| txt += tmpl(); | |
| }; | |
| return txt | |
| }; | |
| document.getElementById("blog").innerHTML = printRss( | |
| //the file: | |
| "rss.php", | |
| //the template to use: | |
| "\ | |
| <article> \ | |
| <h3> \ | |
| <time>{{updated}}</time> \ | |
| </h3> \ | |
| {{content}} \ | |
| </article> \ | |
| " | |
| ); | |
| </script> | |