32

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>
1
  • 1
    I resolved this! It was an extra )
    – Adam A
    Feb 10, 2017 at 23:16

16 Answers 16

16

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
1
  • 5
    I just came to say I resolved it. It was an extra parenthesis
    – Adam A
    Feb 10, 2017 at 22:48
6

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

var fun = fun()

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;

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

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

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'

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.

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.

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.

1
0

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.

0

A reoccurring issue I keep having is Chrome's near over-the-top caching and refusal to update scripts. I added a new function to a separate script file and was having this exact problem. Using Chome's Dev tools I discovered that the script file which should've had the function in it didn't, and this was because it was using a cached version of the script from before I added the new function.

With the Dev Tools (Inspect Element) open, right-click the refresh button and choose Empty Cache and Hard Reload

0

you can make setinterval and clear interval

         var refreshIntervalId =setInterval(function () {
                    YourFunction();
 if (typeof YourFunction != undefined){
     clearInterval(refreshIntervalId);
    }
}, 500);

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Nov 15, 2022 at 7:07
0

Remove type in the script tag. I added type="module" for import and export. But because of this, some code that was not compatible with module type will be skipped. So the error method undefined will be given.

-2

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

1
  • 1
    Would be better if you describe more about the answer. Jan 12, 2021 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.