开发者

Embed RSS into HTML

I would like to embed an RSS开发者_Go百科 feed from a site of mine into another site. Is there any free service that can do this for me or a way I can insert the HTML or javaScript?


JavaScript will not be able to load an RSS feed from a different domain; a page on domain A is not allowed to make a simple GET request to domain B because of security restrictions. However, if you build a proxy under the same domain using your server-side language of choice, your JavaScript can load the content from there. Here's a really simplified example using jQuery on the client and ASP.NET on the server.

Client:

$.get('Proxy.ashx?feed=http://stackoverflow.com/feeds', function(data) {
    // Do something with the feed
});

Server:

public class Proxy : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        using (var webClient = new WebClient())
        {
            context.Response.Write(
                webClient.DownloadString(context.Request.QueryString["feed"]));
        }
    }
}


You can provide the link to the current page's RSS like so:

<link rel="alternate" type="application/atom+xml" href="link_here">

But to have it visible in a web page, you'll need to use at least a server side script if no JavaScript is used. It's not available in plain HTML.


You can easily use jQuery to pull the RSS (its an XML Format) with a $.ajax('http://pathToRssFeed') and then format it and put it into the page using something like jQuery Templates. It's very simple.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜