简体   繁体   中英

JavaScript Function Not Defined Error (BUT IT IS DEFINED)

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.

<%@ 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>

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

这也发生了,因为我试图调用一个具有相同变量名的函数。

var fun = fun()

well probably that's the reason why: Uncaught ReferenceError: function is not defined with onclick

As an short answer: "Never use .onclick(), or similar attributes [onblur()] from a userscript!"

what let me to this question is that I had an empty function in my namespace

and when I called that function, I had this error that causes the whole page to stop working

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;

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.

'function xxx is undefined'

在我的情况下(我使用asp:UpdatePanel ),如果脚本仅在async post back后出现在页面中,它将不会被执行,也不会被任何 JavaScript 或浏览器控制台( undefined )访问,所以我切换任何button结果在回发sync post back出现一个新脚本以sync post back

我不得不放

window.addEventListener('DOMContentLoaded', function() {callFUnction()});

That happened to me because I already had a var named reset, and the function name was also reset.

var reset = document.querySelector();

function reset() {
};

In case anyone has the same issue.

My problem was that I had the await keyword but the function did not have async in front of the function declaration. It was causing all of the functions to throw the undefined error.

尝试从<script>标签中删除type="text/javascript"

我尝试将函数绑定到 window 对象,以便它工作!

window.func = arg => console.log('=\\')

I have come across this kind of error very often.

First of all I will check if I am calling the right function, if everything is fine then I do cache clear and reload it works for me everytime.

Long press reload button and then do cache clear

Hope this works... :)

I was using PHP and got same problem. I solved the problem by defining the function in a file and then including it on the top of my master page.

I was getting error because my function code had error in it. Please check for any error message in browsers console panel. Hope it will help. Happy coding.

At the end of the day, your server side code does not matter. I could've been written in any other server side language.

If you are seeing that error on the browser you need to check the javascript code that was generated by your server-side code.

Once there you will be able to see if the function is really defined or not. If not, something is wrong with the server-side javascript code generation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM