Can't Include External Javascript In File

亡梦爱人 提交于 2019-12-13 14:52:22

问题


I'm new to PHP and VERY, VERY new to any sort of server administration. I'm running from XAMPP 3.1.0 for Windows and using PHP Version 5.4.

My PHP script is executing just fine, but for whatever reason I can't seem to include external js files like so:

<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.]

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.]

<!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.

Here's the script debugger, showing no file loaded:

Finally, this is my Net tab, showing that the file has been downloaded:

[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.]

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();

回答1:


After seeing your screenshots,

  1. When you use a src="" attribute, you are supposed to leave the body of the <script> tag empty. So remove that alert("I am here, aren't you!") from there.
  2. Your core.js is not found at the path. Let us know the folder path.



回答2:


  • Check your console for errors (CTRL+SHIFT+I for Chrome/Windows).
  • It's possibly a 404 error or an error in your core.js file. Make sure the path is correct: <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>




回答3:


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...



来源:https://stackoverflow.com/questions/13655949/cant-include-external-javascript-in-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!