简体   繁体   English

JavaScript-编写未显示的html元素?

[英]JavaScript - writing html elements that don't show up?

I'm trying to write some code in javascript that uses a user's cookies to display a box containing some information. 我正在尝试在JavaScript中编写一些代码,该代码使用用户的Cookie来显示包含某些信息的框。 The page opens with some boxes containing news articles from Google News RSS feeds. 该页面将打开,其中包含一些框,其中包含来自Google新闻RSS供稿的新闻报道。 I'm using a 3rd party app for the RSS; 我使用RSS的第三方应用程序; the feed is included in the HTML code as such: 该供稿包含在HTML代码中,如下所示:

<script language="JavaScript" src="http://www.feedroll.com/rssviewer/feed2js.php?src=http%3A%2F%2Fnews.google.com%2Fnews%3Fq%3Dbarack%2Bobama%26output%3Drss&num=4&date=y&targ=y&utf=y&css=feed"  charset="UTF-8" type="text/javascript"></script>

The user can move the boxes around, and I want to store the box locations using cookies so that if a user revisits the page, the boxes will be in the same location. 用户可以四处移动盒子,我想使用Cookie存储盒子的位置,这样,如果用户重新访问页面,盒子将位于同一位置。 However, when I try to load the page from the information in the cookies, the boxes are blank. 但是,当我尝试从cookie中的信息加载页面时,这些框为空白。 This is an example of what my code looks like (RSS feed for news using keyword "Barack Obama"): 这是我的代码的示例(使用关键字“ Barack Obama”的RSS提要):

// Render boxes into HTML
function renderItem(container) {
    var wrapper = document.getElementById(container);

    var div_box = document.createElement('div');

    var feed_url = 'http://www.feedroll.com/rssviewer/feed2js.php src=http%3A%2F%2Fnews.google.com%2Fnews%3Fq%3Dbarack%2Bobama%26output%3Drss&num=4&date=y&targ=y&utf=y&css=feed';

    var div_box_feed = document.createElement('div');
    var feed_script = document.createElement('script');
    feed_script.setAttribute('language', 'JavaScript'); 
    feed_script.setAttribute('src', feed_url);
    feed_script.setAttribute('charset', 'UTF-8');
    feed_script.setAttribute('type', 'text/javascript');

    div_box_feed.appendChild(feed_script);
    div_box.appendChild(div_box_feed);

    wrapper.appendChild(div_box);
}

When the page loads, the box appears but the news articles from the RSS feed are not there and the box is empty. 页面加载时,将出现该框,但RSS提要中的新闻文章不存在,并且该框为空。 When I look at the source code, however, it is identical to the code of the initial boxes (which did display the news articles). 但是,当我查看源代码时,它与初始框(确实显示新闻文章)的代码相同。

Does anybody know what's wrong? 有人知道怎么了吗?

Thanks! 谢谢!

You never declared column_div here: 您从未在这里声明column_div

wrapper.appendChild(column_div);

Did you mean: 你的意思是:

wrapper.appendChild(div_box);

?

I believe that by default script elements created in this manner have their async attribute set to true , so the rest of your code will execute before your JS has been retrieved. 我相信默认情况下,以这种方式创建的脚本元素的async属性设置为true ,因此其余代码将在检索JS之前执行。

Where does the column_div variable come from? column_div变量从何而来?

You're missing a question mark to separate the post variables from the URL, so the URL is resolving to 您缺少一个问号来将发布变量与URL分开,因此URL解析为

var feed_url = 'http://www.feedroll.com/rssviewer/feed2js.php%20src=http%3A%2F%2Fnews.google.com%2Fnews%3Fq%3Dbarack%2Bobama%26output%3Drss&num=4&date=y&targ=y&utf=y&css=feed';

It should be 它应该是

var feed_url = 'http://www.feedroll.com/rssviewer/feed2js.php?src=http%3A%2F%2Fnews.google.com%2Fnews%3Fq%3Dbarack%2Bobama%26output%3Drss&num=4&date=y&targ=y&utf=y&css=feed'

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

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