不能在文件中包含外部Javascript [英] Can't Include External Javascript In File

查看:112
本文介绍了不能在文件中包含外部Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的新手,非常适合任何类型的服务器管理。我使用XAMPP 3.1.0 for Windows并使用PHP版本5.4。



我的PHP脚本执行得很好,但无论出于何种原因,我似乎无法包括如下的外部js文件:

 < script type =text / javascriptsrc =core.js> < /脚本> 

然而,我可以做到这一点没有问题。

 < script type =text / javascript> 
警报(有些警报);
< / script>

有谁知道发生了什么事?

[编辑:这是我的文件夹结构。我的文件路径是:
C:\xampp\htdocs\AllocatedSpendingPlan\ - 它们都位于根目录下。]





以下是我的档案:



[编辑:我使用src属性从script标签主体中删除了代码,但它仍然无法使用。]

 <!DOCTYPE html> 
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< title>< / title>
< script type =text / javascriptsrc =core.js>< / script>
< script type =text / javascript>
alert(working);
< / script>
< / head>
< body>
这里有东西。
< / body>
< / html>

当我查看Firefox中的Net标签时,显示文件已下载,但没有的脚本正在执行,并且当我进行调试时文件本身并未加载。



以下是脚本调试器,显示没有加载文件:





最后,这是我的Net标签,显示文件已被下载: $ b



[编辑<强>:修正。这是我的命名空间声明中的一个错误。我声明我的var是一个函数,当它应该是一个对象文字。]



这是正确的代码。一切都很好。

  var Core = {
namespace:function(ns){
var part = 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]];
}
返回对象;
}
};

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. 当您使用 src =属性时,您应该离开< script> 标签为空。因此,从那里删除 alert(我在这里,不是你!)
  2. 您的 core.js 在路径中找不到。让我们知道文件夹路径。


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

解决方案

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.

这篇关于不能在文件中包含外部Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆