2012-08-14 54 views
0

我正在尝试以下操作。我有这样的代码在我的页脚文件:加载包含Javascript的外部PHP文件

footer.php:

<script type="text/javascript"> 
$(document).ready(function(){ 
$.ajax({ 
url: '', 
type: 'GET', 
dataType: "script", 
success: function(data){ 
    //data is returned back 
    $('#latestforumposts').html(data); 
} 

}); 

}); 
</script> 

这是文件forumposts.php:

<script type="text/javascript" src='http://www.habboxforum.com/external.php?forumids=1382,1384,4,5,14,1410,7,85,40,43,124,123,24,30,306,34,446,38,214,1409,249,767,69,71,73,134,56,45,1230,54,135,1424,1425,601,893,209,1086&type=js'></script> 

<script type="text/javascript"> 
<!-- 
for (x = 0; x < 5; x++) 
{ 
document.writeln("<tr class='forumnewposts'><td><div class='forumpostwidth'><a target='_blank' href='http://www.habboxforum.com/showthread.php?t="+threads[x].threadid+"'>"+threads[x].title+"</a></div></td><td><div class='forumuserwidth'>"+threads[x].poster+"</div></td></tr>"); 
} 
//--> 
</script> 

基本上,文件似乎并没有加载在div #latestforumposts像它的意思,但如果你直接访问forumposts.php页面,它显示了它应该的内容(例如在forumposts.php中的脚本)。

如何拖动该脚本并将其放入我的页面上的div。我需要这样做的原因是我需要它每x秒钟自动刷新一次。

UPDATE:

好了,所以我现在在我的页脚下面的代码:

<script type="text/javascript" src="http://www.habboxforum.com/external.php?forumids=1382,1384,4,5,14,1410,7,85,40,43,124,123,24,30,306,34,446,38,214,1409,249,767,69,71,73,134,56,45,1230,54,135,1424,1425,601,893,209,1086&type=js"></script> 
    <script type="text/javascript"> 
$(document).ready(function(){ 
$.ajax({ 
url: '', 
type: 'GET', 
dataType: "html", 
success: function(data){ 
    //data is returned back 
    $('div#latestforumposts').html(data); 
} 

}); 

}); 
</script> 

这就是现在加载脚本,但不是显示在到div加载HTML #latestforumposts它只是刷新整个页面的内容。任何想法为什么它不加载到div?

+0

使用'html'为'dataType' – 2012-08-14 09:42:38

+0

这只是不断给我的线程没有定义的错误。这是从该external.php文件中提取的。 好的,所以我加了一行: 就在ajax脚本之前,所以线程被定义了,但现在网站刷新到该php内容,而不是将其加载到正确的div中。 – Tenatious 2012-08-14 09:46:52

+0

用更新后的页面编辑我的原始问题。 – Tenatious 2012-08-14 09:53:38

回答

0

jQuery的

$(document).ready(function() 
{ 
    update(false); 
}); 

function update(load) 
{ 
    if(load) 
    { 
     $.get(url,function(data) 
     { 
      $('#latestforumposts').html(data); 
     }); 
    } 

    setTimeout(function(){update(true)},5000); 
} 

PHP(/forumposts.php)

<script type="text/javascript" src="http://www.habboxforum.com/external.php?forumids=1382,1384,4,5,14,1410,7,85,40,43,124,123,24,30,306,34,446,38,214,1409,249,767,69,71,73,134,56,45,1230,54,135,1424,1425,601,893,209,1086&type=js"></script> 
<script type="text/javascript"> 
    for(x = 0; x < 5; x++) 
    { 
     $('#new_posts tbody').append('<tr class="forumnewposts"><td><div class="forumpostwidth"><a target="_blank" href="http://www.habboxforum.com/showthread.php?t='+threads[x].threadid+'">'+threads[x].title+'</a></div></td><td><div class="forumuserwidth">'+threads[x].poster+'</div></td></tr>'); 
    } 
</script> 
<table id="new_posts"><tbody></tbody></table> 

HTML

<div id="latestforumposts"></div> 
+0

但事情是,VBulletin已经具有内置的功能来保存必须通过数据库。 – Tenatious 2012-08-14 10:06:18

+0

哎呀,对不起,我不知道。也许我可以想到别的。 – snuffn 2012-08-14 10:20:41

+0

更新了我的答案。 :) – snuffn 2012-08-14 10:45:36