.js文件未在文件夹中包含的页面上调用

时间:2015-04-26 16:29:21

标签: javascript php mobile

在每个页面的底部,我有一个.php包含指向我所有.js文件的链接。

<?php include 'Core/js.php';?>

在这个.php中我有这段代码;

<script src="../js/jquery.min.js"></script>
<script src="../js/jquery.dropotron.min.js"></script>
<script src="../js/skel.min.js"></script>
<script src="../js/skel-layers.min.js"> </script>
<script src="../js/init.js"></script>
<script src="../js/slider.js"></script>

这适用于放在我的根文件夹中的页面,即“index.php”

然而,位于文件夹中的页面,当我使用.php包含时,似乎没有调用javascript;

 <?php include '../../../Core/js.php';?>

虽然,当我不使用include功能时,只需粘贴&lt;脚本&gt;,它称之为完美。这对我来说不是一个大问题,但它不允许移动网站正常运行。

诸如“index.php”的第一页具有mobile navigation,而位于文件夹中且没有包含php的页面包含代码don't have the same user friendly navigation。如果有人可以帮我解决这个问题,那就太好了!

3 个答案:

答案 0 :(得分:3)

我认为你的问题与路径有关。

当您在浏览器中执行:<script src="../js/jquery.min.js"></script>时,它会查看URL并从那里开始。让我们说你在http://example.com/products/index.php。浏览器将尝试从http://example.com/products/../js/jquery.min.js加载JS,http://example.com/js/jquery.min.js

为避免这种情况,您应该使用绝对路径,例如:

<script src="/js/jquery.min.js"></script>

然后,它将始终尝试独立于当前网址加载http://example.com/js/jquery.min.js

对于PHP包含,我建议你在包含文件时使用绝对路径。有许多策略,例如使用$_SERVER['DOCUMENT_ROOT'],使用dirname()函数,使用包含路径的全局变量等等。

无论您选择什么,您的包含应该类似于:

<?php include '/var/www/includes/Core/js.php'; ?>

答案 1 :(得分:0)

我假设您的文件夹结构是这样的:

package main

import "fmt"

func iter(min, max uint64) {
    for i, next := min, min <= max; next; i, next = i+1, i < max {
        fmt.Printf("%#016[1]x ", i)
    }
    fmt.Println()
}

func main() {
    const maxUint64 = ^uint64(0)
    iter(0, 3)
    iter(10, 9)
    iter(maxUint64-2, maxUint64)
}

所以,如果你在0x0000000000000000 0x0000000000000001 0x0000000000000002 0x0000000000000003 0xfffffffffffffffd 0xfffffffffffffffe 0xffffffffffffffff - >&gt; / index.php js jquery.min.js jquery.dropotron.min.js skel.min.js ... Core js.php content some folder HTML files ... - &gt; content some folder应该反映出这一点:

html file

因为您可以看到路径发生变化以及您的错误来自哪里

答案 2 :(得分:0)

使用绝对路径。您的绝对路径是服务器上的实际位置。找到它的简单方法是查看您使用ftp连接的路径。

它可能看起来像这样/home/username/public_html/Core/js.php