2015-05-29 180 views
1

我开发了一个使用php(chat.php)的聊天窗口小部件并希望将其添加到另一个php页面(blank.php)中。聊天。PHP需要脚本,我把JavaScript文件放在外面。当加载需要其他JavaScript文件的php文件时,Javascript未加载

所以在blank.php中,我包含了chat.php需要的js文件。

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <!--code--> 
</head> 

<body> 
    <div id='includedContent'></div> 

<!--dependency for chat.php--> 
<link type="text/css" href="http://localhost/chatbox/gplus/chat.css" rel="stylesheet" /> 
<link type="text/css" href="http://localhost/chatbox/gplus/bootstrap.min.css" rel="stylesheet" /> 
<script type="text/javascript" src="http://localhost/chatbox/gplus/bootstrap.min.js"></script> 
<script type="text/javascript" src="http://localhost/chatbox/gplus/jquery.min.js"></script> 
<script type="text/javascript" src="http://localhost/chatbox/gplus/chat.js"></script> 
<script type="text/javascript" src="http://localhost/chatbox/gplus/moment.min.js"></script> 
<!--end--> 

<!--include chat.php--> 
<script> 
    $("#includedContent").load("http://localhost/chatbox/gplus/chat.php"); 
</script> 
<!--end--> 

</body> 
</html> 

chat.php已加载,但Javascript无法加载chat.php。

我还试图用这个和它的作品

<!--include chat.php--> 
<?php 
    $chat = file_get_contents('http://localhost/chatbox/gplus/chat.php'); 
    echo $chat; 
    //or by using - include './chat.php'; 
?> 
<!--end--> 

但我不知道是否有使用JavaScript,所以我可以包括chat.php到blank.php的解决方案,并chat.php可以执行JS包含在blank.php中的脚本。我把脚本放在blank.php中以避免小部件和托管页面之间的脚本冲突。

谢谢。

回答

1

我认为你需要使用的是include语句在PHP中。 See this LINK

关于如何使用基本例如:

对于vars.php:

<?php 
$color = 'green'; 
$fruit = 'apple'; 
?> 

包括在test.php的:

<?php 
echo "A $color $fruit"; // A 
include 'vars.php'; 
echo "A $color $fruit"; // A green apple 
?> 
+0

谢谢,我已经试过了,它的工作原理。但我想用JavaScript来包含其他页面。 – Dewanta

+0

如果您使用'include',则可以执行包含在您所包含页面的JavaScript。 – juntapao