简体   繁体   English

JS包含在包含的.php文件中

[英]JS Include inside included .php file

I have included a PHP file on my mainpage, inside the php file there's another include of a .js file, the included php file is in a different directory than the mainpage, so the included .js file can't be found. 我已经在主页上包含一个PHP文件,在php文件中还包含一个.js文件,所包含的php文件与该主页位于不同的目录中,因此找不到所包含的.js文件。

To visualize: 可视化:

  • Mainpage - Root 主页-根目录
  • Included PHP file -File A located in Folder A 包含的PHP文件-位于文件夹A中的文件A
  • Included JS file in File A is located in Folder A 文件A中包含的JS文件位于文件夹A中

But it's looking for the .js file in Root instead of Folder A 但是它正在根目录中查找.js文件而不是文件夹A

How do I get the path right ? 我如何找到正确的道路?

Also it should be dynamic, I might not know the actual name of Folder A 也应该是动态的,我可能不知道文件夹A的实际名称

Use the .. notation to traverse folders 使用..表示法遍历文件夹

the path would be '../script.js' 路径将是'../script.js'

Site: www.yoursite.com 网站:www.yoursite.com

Folders: 资料夹:

  • /index.php /index.php
  • /lib/header.php /lib/header.php
  • /contacts/index.php /contacts/index.php
  • /js/main.js /js/main.js

Let's say you want to inclue your /js/main.js file inside /lib/header.php in order to work wherever you include /lib/header.php, for example in /index.php and /contacts/index.php. 假设您想将/js/main.js文件包含在/lib/header.php中,以便在包含/lib/header.php的位置(例如在/index.php和/contacts/index.php中)工作。

You would have the following inside /lib/header.php : 您将在/lib/header.php中包含以下内容:

<script src="http://www.yoursite.com/js/main.js" type="text/javascript"></script>

if include.php and xyz.js are in the same directory. 如果include.php和xyz.js在同一目录中。 than

include .js file in include.php like that-- 这样在include.php中包含.js文件-

<script type="text/javascript" language="javascript" src="xyz.js"></script>

if it is not working than u should use Relative Url/SITE URL like that-- 如果它不起作用,那么您应该使用相对网址/站点网址,例如-

<script type="text/javascript" language="javascript" src="http://localhost/folder/xyz.js"></script>

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

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