25

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>
mason
  • 30,041
  • 9
  • 73
  • 113
Adam A
  • 261
  • 1
  • 3
  • 4

13 Answers13

9

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
  • 3
    I just came to say I resolved it. It was an extra parenthesis – Adam A Feb 10 '17 at 22:48
  • Hi @Marvin, my second point is to make sure there is no code inside the asp:contentplaceholder tag of your master page since this is just a place holder tag that gets overwritten once the site renders by the actual content of the desired page. – Jimmy Hodgson Apr 06 '20 at 03:03
  • What if it works with console, but not on the page? – klm123 Apr 03 '21 at 17:48
6

This also happened because I was trying to call a function which had a same variable name.

var fun = fun()

Deke
  • 4,087
  • 2
  • 40
  • 58
1

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;

Basheer AL-MOMANI
  • 13,431
  • 9
  • 89
  • 88
1

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... :)

Mohammed Khurram
  • 615
  • 1
  • 5
  • 13
0

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!"

peterPP1
  • 91
  • 1
  • 8
0

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'

Thilina Nakkawita
  • 1,382
  • 4
  • 19
  • 33
0

In my case (I'm using asp:UpdatePanel), if the script appear in the page only after an async post back it will not be executed nor be accessible by any JavaScript or browser console (undefined), so I switch any button result in a new script appears after post back to sync post back.

Hizabr
  • 393
  • 4
  • 5
0

I had to put

window.addEventListener('DOMContentLoaded', function() {callFUnction()});
Marvin
  • 786
  • 1
  • 11
  • 35
0

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() {
};
0

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.

Darth Scitus
  • 415
  • 1
  • 9
  • 19
0

I tried binding the function to window object so that It worked!

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

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.

Safeer Ahmed
  • 487
  • 4
  • 14
-1

Try to remove type="text/javascript" from the <script> tag.

user1857492
  • 622
  • 1
  • 7
  • 20