2013-04-05 31 views
0

我有一個外部Javascript文件,我已鏈接到我的html文件。定位工作正常,直到我嘗試使用外部JavaScript文件。當我鏈接到頭部時,RSS源就會進入我的頁面頂部。我可以把鏈接放在我的html文件的任何地方,或者它必須在頭上。我希望RSS源位於「新聞源」標識下。我也遺漏了我的頁面的一些信息,因爲我只需要顯示div和css。鏈接到外部JavaScript文件時定位RSS提要

這裏是我的html代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
    <title>GamerZone</title> 
    <link type="text/css" rel="stylesheet" href="index.css" /> 
    <link rel="shortcut icon" type="icon/ico" href="images/icon.ico" /> 
    <script type="text/javascript" src="rss-feed.js"></script> 
</head> 
<body> 
    <div id="allcontent"> 
     <div id="banner"> 
      <a target="_self" href="index.html" title=""> 
      <img src="images/banner.jpg" border="0" alt="GamerZone Banner"> 
      </a> 
     </div> 
     <div class="container"> 
      <ul class="nav"> 
       <li class="nav-home"> 
       <a href="index.html">Home</a> 
       </li> 
       <li class="nav-games"> 
       <a href="games.html">Games</a> 
       </li> 
       <li class="nav-chat"> 
       <a href="chat.html">Chat</a> 
       </li> 
       <li class="nav-suggestions"> 
       <a href="suggestions.html">Suggestions</a> 
       </li> 
      </ul> 
     </div> 
     <div id="newsfeed"> 
      //RSS feed goes here 
     </div> 
     <div id="footer"> 
     </div> 
    </div> 
</body> 
</html> 

這裏是我的CSS代碼:

#allcontent { 
    width: 1000px; 
    padding-top: 5px; 
    padding-bottom: 5px; 
    background-color: ; 
    margin-left: auto; 
    margin-right: auto; 
} 

#poll { 
    background-color: white; 
    float: left; 
    padding: 0px 5px 5px 5px; 
    margin: 15px 10px 10px 22px; 
    font-family: Tahoma; 
} 

#newsfeed { 
    padding: 15px; 
    margin: 0px 10px 10px 10px; 
} 

這裏是我的Javascript代碼:

<!-- 
rssfeed_url = new Array(); 
rssfeed_url[0]="http://news.yahoo.com/rss/gaming"; 
rssfeed_frame_width="500"; 
rssfeed_frame_height="720"; 
rssfeed_scroll="on"; 
rssfeed_scroll_step="6"; 
rssfeed_scroll_bar="on"; 
rssfeed_target="_blank"; 
rssfeed_font_size="14"; 
rssfeed_font_face=""; 
rssfeed_border="on"; 
rssfeed_css_url="http://example.com/newsfeed.css"; 
rssfeed_title="on"; 
rssfeed_title_name=""; 
rssfeed_title_bgcolor="#3366ff"; 
rssfeed_title_color="#fff"; 
rssfeed_title_bgimage="http://"; 
rssfeed_footer="off"; 
rssfeed_footer_name="rss feed"; 
rssfeed_footer_bgcolor="#fff"; 
rssfeed_footer_color="#333"; 
rssfeed_footer_bgimage="http://"; 
rssfeed_item_title_length="50"; 
rssfeed_item_title_color="#666"; 
rssfeed_item_bgcolor="#fff"; 
rssfeed_item_bgimage="http://"; 
rssfeed_item_border_bottom="on"; 
rssfeed_item_source_icon="off"; 
rssfeed_item_date="off"; 
rssfeed_item_description="on"; 
rssfeed_item_description_length="120"; 
rssfeed_item_description_color="#666"; 
rssfeed_item_description_link_color="#333"; 
rssfeed_item_description_tag="off"; 
rssfeed_no_items="0"; 
rssfeed_cache = "7872565d53aadc6c14b4ab1bb3f79bdb"; 
//--> 

(function(){ 
var a=window; 
var b=""; 
for(i=0;i<a.rssfeed_url.length;i++) { 
    b=b+"rssfeed[url]["+i+"]="+encodeURIComponent(a.rssfeed_url[i])+"&" 
} 
var c="http://feed.surfing-waves.com/php/rssfeed.php"+"?"+b+"rssfeed[type]="+(a.rssfeed_type?a.rssfeed_type:"")+"&rssfeed[frame_width]="+a.rssfeed_frame_width+"&rssfeed[frame_height]="+a.rssfeed_frame_height+"&rssfeed[scroll]="+(a.rssfeed_scroll?a.rssfeed_scroll:"")+"&rssfeed[scroll_step]="+(a.rssfeed_scroll_step?a.rssfeed_scroll_step:"")+"&rssfeed[scroll_bar]="+(a.rssfeed_scroll_bar?a.rssfeed_scroll_bar:"")+"&rssfeed[target]="+(a.rssfeed_target?a.rssfeed_target:"")+"&rssfeed[font_size]="+(a.rssfeed_font_size?a.rssfeed_font_size:"")+"&rssfeed[font_face]="+(a.rssfeed_font_face?a.rssfeed_font_face:"")+"&rssfeed[border]="+(a.rssfeed_border?a.rssfeed_border:"")+"&rssfeed[css_url]="+(a.rssfeed_css_url?encodeURIComponent(a.rssfeed_css_url):"")+"&rssfeed[title]="+(a.rssfeed_title?a.rssfeed_title:"")+"&rssfeed[title_name]="+(a.rssfeed_title_name?a.rssfeed_title_name:"")+"&rssfeed[title_bgcolor]="+(a.rssfeed_title_bgcolor?encodeURIComponent(a.rssfeed_title_bgcolor):"")+"&rssfeed[title_color]="+(a.rssfeed_title_color?encodeURIComponent(a.rssfeed_title_color):"")+"&rssfeed[title_bgimage]="+(a.rssfeed_title_bgimage?encodeURIComponent(a.rssfeed_title_bgimage):"")+"&rssfeed[footer]="+(a.rssfeed_footer?a.rssfeed_footer:"")+"&rssfeed[footer_name]="+(a.rssfeed_footer_name?a.rssfeed_footer_name:"")+"&rssfeed[footer_bgcolor]="+(a.rssfeed_footer_bgcolor?encodeURIComponent(a.rssfeed_footer_bgcolor):"")+"&rssfeed[footer_color]="+(a.rssfeed_footer_color?encodeURIComponent(a.rssfeed_footer_color):"")+"&rssfeed[footer_bgimage]="+(a.rssfeed_footer_bgimage?encodeURIComponent(a.rssfeed_footer_bgimage):"")+"&rssfeed[item_bgcolor]="+(a.rssfeed_item_bgcolor?encodeURIComponent(a.rssfeed_item_bgcolor):"")+"&rssfeed[item_bgimage]="+(a.rssfeed_item_bgimage?encodeURIComponent(a.rssfeed_item_bgimage):"")+"&rssfeed[item_title_length]="+(a.rssfeed_item_title_length?a.rssfeed_item_title_length:"")+"&rssfeed[item_title_color]="+(a.rssfeed_item_title_color?encodeURIComponent(a.rssfeed_item_title_color):"")+"&rssfeed[item_border_bottom]="+(a.rssfeed_item_border_bottom?a.rssfeed_item_border_bottom:"")+"&rssfeed[item_source_icon]="+(a.rssfeed_item_source_icon?a.rssfeed_item_source_icon:"")+"&rssfeed[item_date]="+(a.rssfeed_item_date?a.rssfeed_item_date:"")+"&rssfeed[item_description]="+(a.rssfeed_item_description?a.rssfeed_item_description:"")+"&rssfeed[item_description_length]="+(a.rssfeed_item_description_length?a.rssfeed_item_description_length:"")+"&rssfeed[item_description_color]="+(a.rssfeed_item_description_color?encodeURIComponent(a.rssfeed_item_description_color):"")+"&rssfeed[item_description_link_color]="+(a.rssfeed_item_description_link_color?encodeURIComponent(a.rssfeed_item_description_link_color):"")+"&rssfeed[item_description_tag]="+(a.rssfeed_item_description_tag?a.rssfeed_item_description_tag:"")+"&rssfeed[no_items]="+(a.rssfeed_no_items?a.rssfeed_no_items:"")+"&rssfeed[cache]="+(a.rssfeed_cache?a.rssfeed_cache:""); 
if(a.rssfeed_border!="off"&&!a.rssfeed_css_url){} 
document.write('<iframe name="rssfeed_frame" width="'+a.rssfeed_frame_width+'" height="'+a.rssfeed_frame_height+'" frameborder="0" src="'+c+'" marginwidth="0" marginheight="0" vspace="0" hspace="0" scrolling="no" ALLOWTRANSPARENCY="true"></iframe>') 
})() 

如果需要任何額外的信息,請拒絕前詢問我的問題。 另外,爲了將來的參考,是否有一種方法可以正確格式化一切,而不會造成一百萬次空間?如果這被視爲一個額外的問題,不允許然後忽略。謝謝。

回答

1

您的腳本使用document.write,因此您應該將script指向<div id="newsfeed">中的RSS JavaScript。該腳本生成IFRAME,我假設surfing-waves.com正在轉換爲HTML,因此將其放入HEAD將在HEAD中寫入IFRAME,您應該避免使用IFRAME。所以,請從HEAD<script type="text/javascript" src="rss-feed.js"></script>並將其移到<div id="newsfeed">這樣的:

<div id="newsfeed"> 
    <script type="text/javascript" src="rss-feed.js"></script> 
</div> 

如果您希望將RSS Feed作爲該頁面的另一種形式關聯,那麼你可以使用LINK元素引用它,但這不適用於此。在這種情況下,您只需要在頁面中顯示這些項目。

+0

這是一個愚蠢的問題,我猜。唯一的另一個問題是,我的科學Sci教授說,JavaScript文件的鏈接應該在頭上。雖然,如果你的方式很好,那麼我只會使用它。 – TheUnCola 2013-04-05 16:06:21

+1

當瀏覽器解析一個JavaScript文件時,它基本上只是將它插入到html中,就好像它是一個腳本塊在它所鏈接的位置。如果該JavaScript文件正在做的東西在頭部沒有意義,那麼該文件不應該從那裏鏈接。 document.write()在運行代碼的文檔中輸出html,如果它添加了應該在主體中的代碼,那麼腳本應該在主體中。 – Racheet 2013-04-05 16:25:58

+0

感謝您的幫助,你們倆。 – TheUnCola 2013-04-05 16:33:15