JavaScript 函数未定义错误(但已定义)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42169588/
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
JavaScript Function Not Defined Error (BUT IT IS DEFINED)
提问by Adam A
I have a JavaScript function which fires on blur. Strangely enough it worked fine the first time I ran it, and ever since then I've been getting an error that says JavaScript Function Not Defined - and it stops running. I have googled around similar problems but none of the advice has been able to resolve the issue. Asp.Net 3.5 Webforms, if it matters. I have included some extra functions and lines of code which may be unrelated to the problem. The issue I'm having regards updateFiscalGrid, the large function. The HTML which binds to the event is below the function.
我有一个在模糊时触发的 JavaScript 函数。奇怪的是,它在我第一次运行时运行良好,从那以后我一直收到一个错误,指出 JavaScript 函数未定义 - 它停止运行。我搜索过类似的问题,但没有任何建议能够解决问题。Asp.Net 3.5 Webforms,如果重要的话。我已经包含了一些可能与问题无关的额外功能和代码行。我遇到的问题是 updateFiscalGrid,这个大函数。绑定到事件的 HTML 位于函数下方。
<%@ Page MasterPageFile="~/MasterPages/NPRPage.Master" CodeBehind="NPRFundingApplication.aspx.cs" Inherits="Tea.Hcf.Web.Secured.NPRFundingApplication" AutoEventWireup="true" Language="C#" EnableEventValidation="true" MaintainScrollPositionOnPostback="true" %>
<%@ Register TagPrefix="ew" Namespace="eWorld.UI" Assembly="eWorld.UI" %>
<%@ Register TagPrefix="hcf" Namespace="Tea.Hcf.Web" Assembly="Tea.Hcf.Web" %>
<%@ Register Src="../Controls/NpCdnSearch.ascx" TagName="NpCdnSearch" TagPrefix="np1" %>
<%@ Register Src="../Controls/NpStudentRoster.ascx" TagName="NpStudentRoster" TagPrefix="np2" %>
<script type="text/javascript" language="javascript">
//<![CDATA[
function showMaxWin(nUrl) {
var h = 600;
var w = 800;
var features = 'resizable=1, width=' + w + ', height=' + h + ', top=0, left=0';
NewWin = window.open(nUrl, 'NewWin', features);
}
function dateChangedCallback() {
updateSubTotals();
}
function updateFiscalGrid(){
var RelatedServicesCost = document.getElementById('<%= RPCB_SUPP_SVCS_SUBTOTAL3.ClientID %>').value;
var ResidentialCare = document.getElementById('<%= RPCB_RES_SVCS_SUBTOTAL3.ClientID %>').value;
var TotalCostforResPlacement = document.getElementById('<%= TotalResidentialPlacement.ClientID %>').value;
var SetAside = document.getElementById('<%= rblSetAsideMet.ClientID %>').value;
var LocalTaxSubtraction = document.getElementById('<%= LocalTaxShareSubtraction.ClientID %>').value;
var IDEABRelatedServiceCost = document.getElementById('<%= RelatedServicesSetAside.ClientID %>').value;
var IDEABDiscretionaryServicesCost = document.getElementById('<%= RelatedServicesDiscretionary.ClientID %>').value;
var IDEABREsidentialCare = document.getElementById('<%= ResidentialCareSetAside.ClientID %>').value;
var IDEABDiscResCare = document.getElementById('<%= ResidentialCareDiscretionary.ClientID %>').value;
var StateFSP = document.getElementById('<%= TotalEducationServices2.ClientID %>').value;
var Discretionary = document.getElementById('<%= DiscretionaryTotal.ClientID %>').value;
var IDEABAward = document.getElementById('<%= IdeaBAward.ClientID %>').value;
if(SetAside = '0'){
Discretionary = LocalTaxSubtraction + IDEABRelatedServiceCost + IDEABDiscretionaryServicesCost + IDEABREsidentialCare + IDEABDiscResCare;
}
else {
Discretionary = LocalTaxSubtraction + IDEABDiscretionaryServicesCost + IDEABDiscResCare;
}
IDEABAward = (RelatedServicesCost + ResidentialCare + TotalCostforResPlacement) - Number(Discretionary));
//IDEABAward = (Number(RelatedServicesCost) + Number(ResidentialCare) + Number(TotalCostforResPlacement)) - Number(Discretionary));
document.getElementById('<%= DiscretionaryTotal.ClientID %>').value = Discretionary;
document.getElementById('<%= IdeaBAward.ClientID %>').value = IDEABAward;
}
//]]>
</script>
<td>
<hcf:CurrencyBox ID="LocalTaxShareSubtraction" OnBlur= "updateFiscalGrid();" Precision="2" runat="server" />
</td>
回答by Jimmy Hodgson
Using browser development tools see if you can call the function manually from the console. If you still get Function not defined then do the following:
使用浏览器开发工具查看是否可以从控制台手动调用该函数。如果仍然没有定义函数,请执行以下操作:
- Check for typos
- If you use a master page, make sure you are not placing the reference of the script page inside the container tags (which get overwritten with the actual page content, this is from personal experience lol)
- Clear your cache and reload the page
- 检查错别字
- 如果您使用母版页,请确保您没有将脚本页面的引用放置在容器标签内(这些标签会被实际页面内容覆盖,这是个人经验,哈哈)
- 清除缓存并重新加载页面
回答by peterPP1
well probably that's the reason why: Uncaught ReferenceError: function is not defined with onclick
很可能这就是原因:Uncaught ReferenceError: function is not defined with onclick
As an short answer: "Never use .onclick(), or similar attributes [onblur()] from a userscript!"
作为一个简短的回答:“永远不要使用 .onclick() 或来自用户脚本的类似属性 [onblur()]!”
回答by Basheer AL-MOMANI
what let me to this question is that I had an empty functionin my namespace
让我回答这个问题的是empty function我的命名空间中有一个
and when I calledthat function, I had this errorthat causes the whole page to stop working
当我使用called那个功能时,我有这个error导致整个页面停止工作
TypeError: MyNamespcae.myFunction is not a function
so don't create an empty function, at lease add one statement like void(0); or return null;
所以不要创建一个空函数,至少添加一个像 void(0); 这样的语句。或返回空;
回答by Deke
This also happened because I was trying to call a function which had a same variable name.
这也发生了,因为我试图调用一个具有相同变量名的函数。
var fun = fun()
var fun = fun()
回答by Julio Jungin Hong
For reference,
以供参考,
In my case, I miss-typed my variable name in function.
就我而言,我在函数中打错了变量名。
var myVar = "";
But in function, it typed included whitespace to 'my Var', then I saw below message on the console.
但是在功能上,它在“我的 Var”中输入了包含空格,然后我在控制台上看到了下面的消息。
'function xxx is undefined'
'函数xxx未定义'
回答by Hizabr
In my case (I'm using asp:UpdatePanel), if the script appear in the page only after an async post backit will not be executed nor be accessible by any JavaScript or browser console (undefined), so I switch any buttonresult in a new script appears after post back to sync post back.
在我的情况下(我正在使用asp:UpdatePanel),如果脚本仅在 之后出现在页面中async post back,它将不会被执行,也不会被任何 JavaScript 或浏览器控制台(undefined)访问,因此我button在回发后出现的新脚本中切换任何结果到sync post back。
回答by Marvin
I had to put
我不得不放
window.addEventListener('DOMContentLoaded', function() {callFUnction()});

