访问不同文件中定义的 javascript 函数
我在访问不同地方定义的 javascript 函数时遇到两个看似相关的问题。我遇到的第一个问题是调用我从 firgbug 或 safari 控制台定义的函数。我定义了一个名为 getRed 的函数,如下所示:
function getRed(row, col)
{
// do something stuff and return the red value as a float
}
我希望能够从控制台测试该函数,但每次我尝试调用 getRed(1,1);例如,我收到如下错误:ReferenceError: getRed is not Defined
我需要进行特殊调用来定义命名空间吗?我在一个名为 Drawing.js 的 javascript 文件中定义了这个函数,该文件很早就在我的 html 页面中定义了。
我遇到的另一个问题是从我的 dojo 调色板的 onChange: 方法调用同一个 Drawing.js 文件中定义的函数。以下是调色板的代码:
<script type="text/javascript" src="drawing.js"></script>
//the method colorChange is inside drawing.js which is defined before the dojo
//color palette
<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dojo/dojo.xd.js"
djConfig="parseOnLoad: true">
</script>
<script type="text/javascript">
dojo.require("dojox.widget.ColorPicker");
dojo.addOnLoad(function() {
var c = new dojox.widget.ColorPicker({
onChange: function(val)
{
console.log("BEFORE");
colorChange(val);
console.log("AFTER");
}
},
"picker1");
});
</script>
这是文件drawing.js 中的changeColor 的定义:
function colorChange(val)
{
console("colorChange!");
}
每次单击调色板时,都会出现以下错误:ReferenceError:colorChange 未定义。
我对 javascript 很陌生,我确信这两个问题有一个非常相似且简单的解决方案,但我无法在网上找到答案。有人可以帮我吗?
我很确定脚本正在加载,如以下屏幕截图所示:
I am having two seemingly related problems accessing javascript function defined in different places. The first problem I am having is calling function I have defined from the console of firgbug or safari. I defined a function called getRed the looks like this:
function getRed(row, col)
{
// do something stuff and return the red value as a float
}
I would like to be able to test this function from the console but every time I try and call getRed(1,1); for example, I get an error like this: ReferenceError: getRed is not defined
Do I need to make a special call to define the namespace? I define this function in a javascript file called drawing.js which is define very early in my html page.
The other problem I am having is calling a function defined in that same drawing.js file from the onChange: method of my dojo color palette. Here is the code for the color palette:
<script type="text/javascript" src="drawing.js"></script>
//the method colorChange is inside drawing.js which is defined before the dojo
//color palette
<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dojo/dojo.xd.js"
djConfig="parseOnLoad: true">
</script>
<script type="text/javascript">
dojo.require("dojox.widget.ColorPicker");
dojo.addOnLoad(function() {
var c = new dojox.widget.ColorPicker({
onChange: function(val)
{
console.log("BEFORE");
colorChange(val);
console.log("AFTER");
}
},
"picker1");
});
</script>
Here is the definition of changeColor inside the file drawing.js:
function colorChange(val)
{
console("colorChange!");
}
Every time I click on the color palette I get the following error: ReferenceError: colorChange is not defined.
I am very new to javascript and I am sure that these two issue have a very similar and easy solution but I have not been able to find the answer online. Can anyone help me out?
I am pretty sure the script is being loaded as this screen shot shows:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
控制台与您的页面位于同一全局范围内。由于
getRed()
和colorChange()
均在 Drawing.js 中定义,并且在全局范围内都找不到,因此我怀疑 Drawing.js 未正确包含。要检查是否确实包含 Drawing.js(即文件路径正确),请转到 Firebug 的“脚本”选项卡。它将列出当前页面上包含的所有脚本。
Console is in the same global scope as your page. Since
getRed()
andcolorChange()
are both defined in drawing.js and neither can be found in the global scope, I suspect drawing.js is not being properly included.To check that drawing.js is actually included (i.e. and you have the file path correct), go to Firebug's Script tab. It'll list all scripts included on the current page.
不需要额外的配置,你只需要确保你的drawing.js被包含,检查文件的路径是否正确。如果正确的话调用应该没有问题。
只需检查您的 js 位置是否真实,如果您的 js 文件位于根目录中,只需在位置前面添加斜杠,这样它将始终从 www.example.com/drawing.js 将属性 language="javascript" 添加到您的
我希望这有帮助。
There is no need for extra configuration, all you need is to make sure that your drawing.js is included, check if the path to the file is correct. If its correct that there should be no problems with the invoke.
Just check is your js location real, if your js file is in root just add slash in front of location so it will load it always from www.example.com/drawing.js Add attribute language="javascript" to your <script ....
I hope this helps.