javascript Microsoft JScript 运行时错误:预期功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8681700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Microsoft JScript runtime error: Function expected
提问by Wilbert Almodovar
i have the following javascript function with some jquery in it. When I call the createAppointmentConfirmfunction, it tries to call the SendMail(id)function but throws an error:
我有以下 javascript 函数,其中包含一些 jquery。当我调用createAppointmentConfirm函数时,它尝试调用SendMail(id)函数但抛出错误:
"Microsoft JScript runtime error: Function expected"
“Microsoft JScript 运行时错误:预期功能”
And Firefox debugger throws an error:
并且 Firefox 调试器抛出错误:
SendMail is not a function [Break On This Error] SendMail(data.Message);
SendMail 不是函数 [Break On This Error] SendMail(data.Message);
Here is the code. Thank you for your help.
这是代码。谢谢您的帮助。
function createAppointmentConfirm(data) {
if (data.Error) {
alert(data.Message);
} else {
if ($('#sendMail').attr('checked') == 'checked') {
SendMail(data.Message);
}
RefreshAppointmentList();
}
}
function SendMail(id) {
$.ajax({
url: $('#sendMail').attr('title'),
data: { id: id },
type: "POST",
beforeSend: function () {
$('#sendingMail').dialog({ modal: true });
},
success: function (data) {
if (data.Error) {
alert(data.Message);
}
$('#sendingMail').dialog("close");
},
error: function () {
alert("Error sending mail");
$('#sendingMail').dialog("close");
}
});
}
回答by ShadowScripter
Explanation
解释
Oh boy, I love these kind of questions, mostly because I get to do some research and example code! :)
哦,天哪,我喜欢这类问题,主要是因为我可以做一些研究和示例代码!:)
So, first off, let's explain what's going on with your code.
所以,首先,让我们解释一下您的代码发生了什么。
There is nothing technicallywrong with your code, it's just that you've got either a naming conflict or the functions you're trying to call are outside of the scope that are calling them. It could also be that you're linking two separate javascript files, and one of them aren't linked in at runtime properly, maybe because of a spelling error.
您的代码在技术上没有任何问题,只是您遇到了命名冲突或您尝试调用的函数超出了调用它们的范围。也可能是您链接了两个单独的 javascript 文件,其中一个在运行时没有正确链接,可能是因为拼写错误。
Here are some articles about the issues:
这里有一些关于这些问题的文章:
Here's a debugging guide, and to be more precise, take a look down the bottom under the heading
Runtime error messages
subheading xyz is not defined
这是调试指南,更准确地说,请查看标题Runtime error messages
副标题下的底部
xyz is not defined
xyz is not defined
This error occurs when your JavaScript code references a variable or object that does not exist. For example, you get the error with the following. (Note that the assigned variable is Test, whereas the variable used with the alert method is Text.)var Test="This is a test; alert (Text);
Another common mistake is to use the wrong capitalization when referring to variables and objects. This code results in an error, even though you have spelled the variable name correctly:
var Test="This is a test; alert (test);
xyz 未定义
当您的 JavaScript 代码引用不存在的变量或对象时,会发生此错误。例如,您收到以下错误。(请注意,分配的变量是 Test,而与 alert 方法一起使用的变量是 Text。)var Test="This is a test; alert (Text);
另一个常见的错误是在引用变量和对象时使用错误的大小写。即使您已正确拼写变量名称,此代码也会导致错误:
var Test="This is a test; alert (test);
Or, as they fail to mention: you might've declared a function and a variable, both with the same name.
或者,正如他们没有提到的那样:您可能已经声明了一个函数和一个变量,它们都具有相同的名称。
Solution
解决方案
The following examples in JSFiddle will trigger errors/run the code when you press the "go" submit button in the HTML panel.
当您按下 HTML 面板中的“go”提交按钮时,JSFiddle 中的以下示例将触发错误/运行代码。
The solution is pretty much up to you. You didn't post the entirety of the code, so I can't really help you spot the exact place.
解决方案几乎取决于您。您没有发布完整的代码,因此我无法真正帮助您找到确切的位置。
If I'd take a guess, I'd say you've declared a variable somewhere with the name SendMail
, and the script assumes you're referring to that one.
如果我猜测一下,我会说您已经在某处用 name 声明了一个变量SendMail
,并且脚本假定您指的是那个变量。
I've created an example where a naming conflict occurs in JSFiddle.
我创建了一个示例,其中在 JSFiddle 中发生命名冲突。
function SendMail(iId) {
//Send the mail information to a server page that will pass it along
//For this example, we'll pretend it returned an object
var oReturned = {
"Error": null,
"Message": "Mail has been sent."
};
SendDebugMessages(oReturned.Message);
}
function SendDebugMessage(msg){
$("#debug").prepend("<div>"+msg+"</div>");
}
Here, I'm trying to call the SendDebugMessage()
function,
but I was a bit careless when I called it, and added an s
so it became SendDebugMessages()
.
Whops.
在这里,我试图调用该SendDebugMessage()
函数,
但是我调用它时有点粗心,添加了一个,s
所以它变成了SendDebugMessages()
.
哎呀。
It can also happen with objects and variables as well. You just won't know where it's coming from.
它也可能发生在对象和变量上。你只是不知道它来自哪里。
Of course, all you need to do to resolve it, is change it back to the correct function name
当然,你需要做的就是把它改回正确的函数名
Here's an examplethat I think represents your problem, a function and a variable sharing the same name.
这是一个我认为代表您的问题的示例,一个函数和一个共享相同名称的变量。
var SendDebugMessage = "something";
function SendDebugMessage(msg){
$("#debug").prepend("<div>"+msg+"</div>");
}
Kind of a straightforward fix: change the name on one of them.
一种简单的解决方法:更改其中一个的名称。