简体   繁体   English

如何将 Javascript 短语(在体内)移动到头部

[英]How do I move a Javascript phrase (within the body) into the head instead

For example I have a code that drags in my twitter feed via a javascript file, I have pasted the links directly in the div I wish for it to appear however my page won't validate (obviously) but I can't figure out how to send it to appear in that div with the Javascript code hidden in the header.例如,我有一个代码通过 javascript 文件拖入我的 twitter 提要,我已将链接直接粘贴到我希望它出现的 div 中,但是我的页面不会验证(显然)但我不知道如何发送它以显示在该 div 中,其中 Javascript 代码隐藏在 header 中。

Below shows the div I need the information in and the javascript files that call the information下面显示了我需要信息的 div 和调用信息的 javascript 文件

Any help would be greatly appreciated !任何帮助将不胜感激 !

<div id="twitter_update_list"><script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/hookline_sinker.json?callback=twitterCallback2&count=1"></script>
<br />FIND US AT <br /><strong><a href="http://twitter.com/#!/hookline_sinker" target="_blank">@HOOKLINE_SINKER</a></strong></div> 

The only reason that I can see for that not to validate is that the ampersands in the URI haven't been HTML encoded (ie as &amp; ).我可以看到不验证的唯一原因是 URI 中的 & 符号没有经过 HTML 编码(即&amp; )。

<script> elements are allowed as child elements of <div> elements. <script>元素可以作为<div>元素的子元素。

If you're using the JavaScript provided by Twitter then I don't think you can.如果您使用的是 Twitter 提供的 JavaScript,那么我认为您不能。

What you're after (if I understand correctly) is an external JavaScript file that you link to in the <head> of your page, that traverses the DOM on page-load, finds a div with an ID of "twitter_update_list" and inserts the content from Twitter.你所追求的(如果我理解正确的话)是一个外部 JavaScript 文件,你在页面的 <head> 中链接到该文件,它在页面加载时遍历 DOM,找到一个 ID 为“twitter_update_list”的 div 并插入来自 Twitter 的内容。

While this functionality is quite easy to code, it would have to be within the JavaScript file itself.虽然此功能很容易编码,但它必须在 JavaScript 文件本身中。 Since you're using Twitter's JavaScript then it's up to Twitter to provide this functionality within their JavaScript.由于您使用的是 Twitter 的 JavaScript,因此 Twitter 在其 JavaScript 中提供此功能。

I think you'd have to look into writing your own JavaScript file that fetched your Twitter RSS feed and parsed it on your page.我认为您必须考虑编写自己的 JavaScript 文件来获取您的 Twitter RSS 提要并在您的页面上对其进行解析。

You might want to look at Remy Sharp's solution - at a brief glance it looks to do what you want: http://remysharp.com/2007/05/18/add-twitter-to-your-blog-step-by-step/您可能想查看 Remy Sharp 的解决方案 - 简要地看一下它看起来可以满足您的需求: http://remysharp.com/2007/05/18/add-twitter-to-your-blog-step-by-步/

<html>
<head>
<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js">
</script>
</head>
<body>
<br />FIND US AT <br /><strong><a href="http://twitter.com/#!/hookline_sinker" target="_blank">@HOOKLINE_SINKER</a></strong>
<div id="twitter_update_list">Updates will go here!</div>
<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/hookline_sinker.json?callback=twitterCallback2&count=1"></script>
</body>
</html>

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

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