简体   繁体   English

使用Javascript从Atom提要中提取图像的来源

[英]Extracting the source of an image from an Atom feed using Javascript

I'm working on a gadget for a Blogger site. 我正在为Blogger网站开发一个小工具。 I want to get the title, link and first image from the first four posts with a specific tag in the site's feed(Atom) using Javascript. 我想使用Javascript从网站的Feed(Atom)中获取前四个帖子中的标题,链接和第一张图片以及特定标签。 So far I have the title and link coming in from these posts an being displayed with HTML in the gadget. 到目前为止,我已经从这些帖子中获得了标题和链接,并在小工具中显示了HTML。 Here is the code: 这是代码:

<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs title="Retrieve Featured Blog Posts" height="150" author="John Behan" />
<Content type="html">
    <![CDATA[
        <script type="text/javascript" src="http://www.google.com/jsapi"></script>
        <script type="text/javascript">
            google.load("feeds", "1");              
        </script>
        <script type="text/javascript">
            function initialize() {

                var feed = new google.feeds.Feed("http://test-ohomind.blogspot.com/feeds/posts/default/-/featured");
                feed.load(function(result) {
                    if (!result.error) {
                        var container = document.getElementById("feed");
                        for (var i = 0; i < result.feed.entries.length; i++) {
                            var entry = result.feed.entries[i];

                            var myElement = document.createElement('div');
                            var content = entry.content;
                            myElement.innerHtml = content;

                            var imagesrc = myElement.getElementsByTagName("img")[0].src;

                            var mydiv = document.getElementById("mydiv");
                            var newcontent = document.createElement('p');

                            newcontent.innerHTML = '<a href="' + entry.link + '">' + entry.title + '</a><br />' + imagesrc;

                            while (newcontent.firstChild) {
                                mydiv.appendChild(newcontent.firstChild);
                            }
                        }
                    }

                });
            }
            window.onload = initialize();
        </script>
            <body>

                <div id="mydiv">

                </div>
            </body>
    ]]>
</Content>
</Module>

The above code gives a blank gadget and Firebug reports that 上面的代码给出了一个空白小工具和Firebug报告

myElement.getElementsByTagName("img")[0] is undefined myElement.getElementsByTagName(“img”)[0]未定义

I've tried changing it around a bit: 我试过改变一下:

var imagesrc = myElement.getElementsByTagName("img");

gives this in the gadget 在小工具中给出了这个

[object NodeList] [对象NodeList]

Can someone show me what I'm doing wrong please? 有人能告诉我我做错了吗? I just need to get the source(src) of the first image. 我只需要获取第一张图像的源(src)。 The rest of the gadget I can do but this little piece of the problem has me totally frustrated at this stage. 我可以做的其余小工具,但这个问题的一小部分让我在这个阶段完全失望。

Thanks in advance. 提前致谢。

one potential issue I've noticed in your code is that your window.onload is actually running the initialize() script function, rather than setting window.onload = initialize or window.onload = function(){initialize()}; 我在你的代码中注意到的一个潜在问题是你的window.onload实际上是在运行initialize()脚本函数,而不是设置window.onload = initializewindow.onload = function(){initialize()}; as I believe you are intending. 因为我相信你的意图。

Given that your code is running before onload, it would explain why getElementsByTagName isn't pulling anything 鉴于您的代码在onload之前运行,它将解释为什么getElementsByTagName没有提取任何东西

Edit: I would also recommend looking at Best practice for using window.onload for best practices 编辑:我还建议您查看使用window.onload获得最佳实践的最佳实践

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM