简体   繁体   English

无法在文件中包含外部Javascript

[英]Can't Include External Javascript In File

I'm new to PHP and VERY, VERY new to any sort of server administration. 我是PHP的新手,非常适合任何类型的服务器管理。 I'm running from XAMPP 3.1.0 for Windows and using PHP Version 5.4. 我正在使用XAMPP 3.1.0 for Windows并使用PHP 5.4版。

My PHP script is executing just fine, but for whatever reason I can't seem to include external js files like so: 我的PHP脚本执行得很好,但无论出于何种原因,我似乎无法包含外部js文件,如下所示:

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

However, I can do this with no problems. 但是,我可以毫无问题地做到这一点。

<script type="text/javascript">
    alert("some alert");
</script>

Does anyone know whats going on? 有谁知道发生了什么?

[ EDIT : Here's my folder structure. [ 编辑 :这是我的文件夹结构。 The path to my files is: C:\\xampp\\htdocs\\AllocatedSpendingPlan\\ - they both live at the root.] 我的文件的路径是:C:\\ xampp \\ htdocs \\ AllocatedSpendingPlan \\ - 它们都存在于根目录。]

文件夹结构

And here is my file: 这是我的档案:

[ EDIT : I removed the code from the body of the script tag with the src attribute, and it still isn't working.] [ 编辑 :我使用src属性从脚本标记的正文中删除了代码,但它仍然无效。]

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        <script type="text/javascript" src="core.js"></script>
        <script type="text/javascript">
            alert("working");
        </script>
    </head>
    <body>
        There is stuff here.
    </body>
</html>

When I look at the Net tab in Firefox, I show that the file has been downloaded, but none of the scripts are executing, and the file itself isn't loaded when I go to debug. 当我查看Firefox中的Net选项卡时,我显示该文件已被下载,但没有任何脚本正在执行,并且在我进行调试时未加载文件本身。

Here's the script debugger, showing no file loaded: 这是脚本调试器,显示没有加载文件:

Firefox的脚本调试器,不显示加载的脚本

Finally, this is my Net tab, showing that the file has been downloaded: 最后,这是我的Net选项卡,显示该文件已被下载:

Firefox的网络选项卡,显示文件已定位和已下载

[ EDIT : Fixed. [ 编辑 :修正。 It was a mistake in my namespace declaration. 我的命名空间声明中的错误。 I declared my var as a function when it should have been an object literal.] 我将var声明为函数,它应该是一个对象文字。]

Here is the correct code. 这是正确的代码。 Everything else is fine. 其他一切都很好。

var Core = {
    namespace: function(ns){
        var parts = ns.split("."),
        object = this,
        i, len;
        for (i=0, len=parts.length; i < len; i++) {
            if (!object[parts[i]]) {
                object[parts[i]] = {};
            }
            object = object[parts[i]];
        }
        return object;
    }
};

Core.namespace("Budgeting.Tools.AllocatedSpending");

Core.Budgeting.Tools.AllocatedSpending = function(){
    return {
        greet: function(){
            alert("hello");
        }
    };
};

var d = new Core.Budgeting.Tools.AllocatedSpending();
d.greet();

After seeing your screenshots, 看到你的截图后,

  1. When you use a src="" attribute, you are supposed to leave the body of the <script> tag empty. 当您使用src=""属性时,您应该将<script>标记的主体留空。 So remove that alert("I am here, aren't you!") from there. 所以从那里删除那个alert("I am here, aren't you!")
  2. Your core.js is not found at the path. 在路径中找不到您的core.js Let us know the folder path. 让我们知道文件夹路径。
  • Check your console for errors (CTRL+SHIFT+I for Chrome/Windows). 检查控制台是否有错误(CTRL + SHIFT + I用于Chrome / Windows)。
  • It's possibly a 404 error or an error in your core.js file. 这可能是您的core.js文件中的404错误或错误。 Make sure the path is correct: <script type="text/javascript" src="/path/to/core.js"></script> 确保路径正确: <script type="text/javascript" src="/path/to/core.js"></script>

  • The script is possibly being included too early/late. 脚本可能包含得太早/太晚。 To test this, make sure that <script> block is in the <head> 要对此进行测试,请确保<script>块位于<head>

It was a simple coding error. 这是一个简单的编码错误。 I declared my namespace as a function instead of an object literal. 我将命名空间声明为函数而不是对象文字。 The code should have read: 代码应该是:

var Core = {
    namespace: function(ns){
        var parts = ns.split("."),
        object = this,
        i, len;
        for (i=0, len=parts.length; i < len; i++) {
            if (!object[parts[i]]) {
                object[parts[i]] = {};
            }
            object = object[parts[i]];
        }
        return object;
    }
};

Instead of: 代替:

var Core = function () {
    namespace: function(ns){
        var parts = ns.split("."),
        object = this,
        i, len;
        for (i=0, len=parts.length; i < len; i++) {
            if (!object[parts[i]]) {
                object[parts[i]] = {};
            }
            object = object[parts[i]];
        }
        return object;
    }
};

Talk about not seeing the forest for the trees... 谈论没有看到森林的树木......

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

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