Lei Wu, Web Developer

Keeping simple things simple.

Display RSS Feed Using JQuery

While working on a static site using Middleman, I need to display an RSS feed using JavaScript.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<script>
    $.ajax({
        type: 'GET',
        url: '/newsfeed.rss',
        dataType: 'xml',
        success: function(data) {
            var liHTML_template = '<li><div class="media"><div class="media-ele"></div><div class="media-bd"><div class="box"><div class="box-hd box-hd_min threeLines"><h4 class="hdg"><a href="{link}" target="_blank">{title}</a></h4></div><div class="box-ft"><p class="txt_miniscule">{pubDate}</p></div></div></div></div></li>';
            var ulHTML = "";
            $(data).find("item").each(function () {
                var title = $(this).find("title").text();
                var link = $(this).find("link").text();
                var pubDate = $(this).find("pubDate").text();

                ulHTML += liHTML_template.replace('{title}', title).replace('{link}', link).replace('{pubDate}', pubDate);
            });

            $('#FeedContainer > div > div.feed-bd > ul').html(ulHTML);

        },
        error: function(jqXHR, textStatus, errorThrown ) {
            console.log(errorThrown);
        }

    });
</script>