如何使用 jQuery 消费和显示 RSS 提要

发布于 2024-12-11 03:28:45 字数 599 浏览 0 评论 0原文

我想使用 jQuery 来使用 RSS 提要,然后创建一些 HTML 内容来定义 RSS 提要中的项目。这对我来说很容易做到。 但是,如果提供 RSS 提要的源发生更改,我还希望我的页面能够自动更新。

例如,假设初始页面加载使用 RSS 提要并且它包含两项:{ George Washington 。

然后,该页面创建两个 HTML 元素(锚标记、div 等),显示“George Washington”和“John Adams”

三十秒后,提供 RSS 提要的源现在包含三个项目:{ George Washington、John Adams 和 Thomas Jefferson }。 如何设置我的页面,以便它知道 RSS 提要已更改,并且需要刷新/重新创建 HTML 元素以反映此更改?

最明显的答案是使用 window.html。 setInterval 并让页面以给定的时间间隔检查 RSS 提要以查看它是否已更改。但我觉得我在这里错过了一些东西,并且有更好的方法来做到这一点。

任何想法将不胜感激。当谈到 RSS/Atom feed 世界时,我的无知令人震惊,所以不要以为我已经知道它(因为我很可能不知道!)并给我你的帮助!

I want to use jQuery to consume an RSS feed, and then create some HTML content that will define the items within the RSS feed. This is easy enough for me to do. However, I also want my page to automatically update should the source that is supplying the RSS feed change.

For example, assume that the intial page load consumes the RSS feed and it contains two items: { George Washington and John Adams }

The page then creates two HTML elements (anchor tags, divs, whatever) that display "George Washington" and "John Adams".

Thirty seconds later the source that supplies the RSS feed now contains three items: { George Washington, John Adams and Thomas Jefferson }. How do I go about setting my page up so that it knows the RSS feed has changed and that it needs to refresh/recreate the HTML elements to reflect this change?

The most obvious answer is to use window.setInterval and have the page check the RSS feed at given intervals to see if it has changed. But I feel like I'm missing something here and that there is a better way to do it.

Any ideas would be greatly appreciated. My ignorance is astounding when it comes to the RSS/Atom feed world, so don't assume that I already know it (because I most likely don't!) and give me your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乄_柒ぐ汐 2024-12-18 03:28:45

除非您可以在 Feed 更新时以某种方式获得回调(Feed 所有者需要提供该回调),并且需要 Node.js 等服务器端解决方案将回调传递给客户端,否则 setInterval< /code> 方式是唯一的方式。

您可以做什么,为了避免每次检查都加载整个提要,您可以首先执行 HEAD 请求并检查 Content-Length 是否与上次请求不同。请记住,这假设了一些事情,例如 Content-Length 将被返回,并且它总是会增加(如果 feed 仅返回最后 10 个元素之类的内容,那么理论上,尽管不太可能,您即使有新文章,也可以获得相同的 Content-Length)。

Unless you can somehow get a callback when the feed is updated, which the feed owner would need to provide, and which would require a server-side solution like Node.js to put the callback through to the client, the setInterval way is the only way.

What you can do, to avoid loading the whole feed every check, you can first do a HEAD request and check if the Content-Length was any different than the last request. Keep in mind this assumes a few things, like that Content-Length will be returned and that it will always increase (if the feed only returns something like the last 10 elements, then theoretically, although unlikely, you could get the same Content-Length even if there was a new article).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文