加载包含Javascript的外部PHP文件

时间:2012-08-14 09:29:34

标签: javascript jquery ajax

我正在努力让以下工作。我在页脚文件中有这段代码:

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秒自动刷新一次。

更新

好的,我现在在页脚中有以下代码:

<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#latestforumposts的html,而是用它刷新整个页面内容。知道为什么它没有加载到div?

1 个答案:

答案 0 :(得分: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>