locked
Uncaught ReferenceError: function is not defined RRS feed

  • Question

  • User134074001 posted

    Uncaught ReferenceError: entr_mes is not defined
    at HTMLInputElement.onclick

    THIS IS THE MESSAGE I AM GETTING WHEN I CLICKING ON THE BUTTON

    CAN I INCLUDE ONLY 1 FUNCTION IN <SCRIPT></SCRIPT> TAGS.

    Here's the code for VIEW with Jquery functions. I have added another function, which is the reason this error is giving.

    @{
        ViewBag.Title = "ContactUs";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <html>
    <head><title>Contact US</title>
        <style>
            header{
         background-color: transparent;
        padding: 30px;
        text-align: left;
        font-size: 45px;
        color: forestgreen;
            }
    
         article {
        /*float: left;*/
        padding: 20px;
        width: 70%;
        background-color: lightblue;
        text-align:center;
        height: 300px; /* only for demonstration, should be removed */
    }
         footer {
        background-color: #777;
        padding: 10px;
        text-align: center;
        color: white;
    }
         .vl{
             border-left: 6px solid;
             height:290px;
             ;
             left:50%;
             margin-left: -3px;
             margin-top:260px;
             top:0;
         }
    
        </style>
        <script>
            function entr_mes() {
                debugger;
                //alert('Your Message is sent');
                var nm = document.getElementById("name").value;
                var em = document.getElementById("email").value;
                var sb = document.getElementById("sub").value;
                var ms = document.getElementById("mes").value;
                $.ajax({
                    type: "POST",
                    contenttype: "multipart/form-data",
                    url: "ContactUs",
                    data: { name: nm.toString(), email: em.toString(), sub: sb.toString(), message: ms.toString() }
                });
            };
            function spa_chk()
            {
                debugger;
                if(@ViewBag.Message!= null)
                { 
                    $('#sp_mes').show();
                    document.getElementById("name").value = "";
                    document.getElementById("email").value = "";
                    document.getElementById("sub").value = "";
                    document.getElementById("mes").value = "";
                }
                else $('#sp_mes').hide();
            };
        
       
        </script>
       
        </head>
    <body>
        <div class="container-fluid" style="width:100%">
            <header>Contact Us</header>
            <article style="float:left;width:50%">
    <strong>Address : </strong>
                <p>
                    128, Parnarshree, Extn<br />
                    Geeta Bhawan<br />
                    Kolkata-700060,India<br />
                </p>
                <br> <strong>Email : </strong><p>aniruddha.mukhopadhyay4dec@gmail.com</p>
            </article>
           <div class="vl"></div>
            <article style="float:right;width:50%">
                <table style="background-color:aquamarine" align="center">
                    <tr><td><span class="align-items-sm-end">Name: </span></td><td width="auto"><input type="text" id="name" /></td></tr>
                    <tr><td><span class="align-items-sm-end">Email Id: </span></td><td><input type="text" id="email" /></td></tr>
                    <tr><td><span class="align-items-sm-end">Subject: </span></td><td><input type="text" id="sub" /></td></tr>
                    <tr><td><span class="align-items-sm-end">Message: </span></td><td><textarea id="mes"></textarea></td></tr>
                    <tr><td><span class="align-items-sm-end"></span></td><td><input id="btnmes" type=submit onclick="return entr_mes()" /></td></tr>
                </table>
                <br />
                <span id="sp_mes" style="color:green">@ViewBag.Message</span>
               
            </article>
            <footer>
                <div class="section mcb-section  full-width  " style="padding-top:0px; padding-bottom:0px; background-color:beige"><div class="section_wrapper mcb-section-inner"><div class="wrap mcb-wrap one  valign-top clearfix" style=""><div class="mcb-wrap-inner"><div class="column mcb-column one column_column "><div class="column_attr clearfix" style=""><iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d921.4883191500388!2d88.29954022918274!3d22.505936138042305!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0x0!2zMjLCsDMwJzIxLjQiTiA4OMKwMTgnMDAuMyJF!5e0!3m2!1sen!2sin!4v1533298392373" width="1000" height="600" frameborder="0" style="border:0" allowfullscreen></iframe></div></div></div></div></div></div><div class="section the_content no_content"><div class="section_wrapper"><div class="the_content_wrapper"></div></div></div><div class="section section-page-footer"><div class="section_wrapper clearfix"><div class="column one page-pager"></div></div></div>
            </footer>
        </div>
    </body>
    </html>
    Monday, August 6, 2018 8:06 AM

Answers

  • User475983607 posted

    aniruddha4

    Sir,

    The problem I am facing is that, I have already defined the function, still its giving function not defined.ACtually there used to be 1 function in a script, but now as I have added another one, the first one is not working and saying :-

    Uncaught ReferenceError: function is not defined

    what to do, how to use both the functions.

    Regards,

    Aniruddha

    The browser is not loading the JavaScript code due to syntax errors.  The first step is to fix the syntax errors as clearly and openly recommended in my first post.  Reposting the same error will not magically change the outcome.   The code might work after fixing the syntax error.  But, you are using a layout page and the posted View has head and style elements.  Make sure you do not have the same function in another file. We cannot see that code and can only guess.

    The code is very difficult to understand due to the many syntax and logical errors.  We can only guess the actual intention.  Please debug your code or explain the intention.

    Below is my best guess.

    @{
        ViewBag.Title = "ContactUs";
    }
    
    <div class="container-fluid" style="width:100%">
        <header>Contact Us</header>
        <article style="float:left;width:50%">
            <strong>Address : </strong>
            <p>
                128, Parnarshree, Extn<br />
                Geeta Bhawan<br />
                Kolkata-700060,India<br />
            </p>
            <br> <strong>Email : </strong><p>aniruddha.mukhopadhyay4dec@gmail.com</p>
        </article>
        <div class="vl"></div>
        <article style="float:right;width:50%">
            <table style="background-color:aquamarine" align="center">
                <tr><td><span class="align-items-sm-end">Name: </span></td><td width="auto"><input type="text" id="name" /></td></tr>
                <tr><td><span class="align-items-sm-end">Email Id: </span></td><td><input type="text" id="email" /></td></tr>
                <tr><td><span class="align-items-sm-end">Subject: </span></td><td><input type="text" id="sub" /></td></tr>
                <tr><td><span class="align-items-sm-end">Message: </span></td><td><textarea id="mes"></textarea></td></tr>
                <tr><td><span class="align-items-sm-end"></span></td><td><input id="btnmes" type=submit onclick="return entr_mes()" /></td></tr>
            </table>
            <br />
            <span id="sp_mes" style="color:green">@ViewBag.Message</span>
    
        </article>
        <footer>
            <div class="section mcb-section  full-width  " style="padding-top:0px; padding-bottom:0px; background-color:beige">
                <div class="section_wrapper mcb-section-inner">
                    <div class="wrap mcb-wrap one  valign-top clearfix" style="">
                        <div class="mcb-wrap-inner">
                            <div class="column mcb-column one column_column ">
                                <div class="column_attr clearfix" style="">
                                    <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d921.4883191500388!2d88.29954022918274!3d22.505936138042305!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0x0!2zMjLCsDMwJzIxLjQiTiA4OMKwMTgnMDAuMyJF!5e0!3m2!1sen!2sin!4v1533298392373" width="1000" height="600" frameborder="0" style="border:0" allowfullscreen></iframe>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="section the_content no_content">
                <div class="section_wrapper">
                    <div class="the_content_wrapper"></div>
                </div>
            </div>
            <div class="section section-page-footer">
                <div class="section_wrapper clearfix"><div class="column one page-pager"></div></div>
            </div>
        </footer>
    </div>
    
    @section scripts{
        <script>
            function entr_mes() {
                var nm = document.getElementById("name").value;
                var em = document.getElementById("email").value;
                var sb = document.getElementById("sub").value;
                var ms = document.getElementById("mes").value;
                $.ajax({
                    type: "POST",
                    contenttype: "multipart/form-data",
                    url: "ContactUs",
                    data: { name: nm.toString(), email: em.toString(), sub: sb.toString(), message: ms.toString() }
                });
            };
            function spa_chk()
            {
                if('@ViewBag.Message' != '')
                {
                    $('#sp_mes').show();
                    document.getElementById("name").value = "";
                    document.getElementById("email").value = "";
                    document.getElementById("sub").value = "";
                    document.getElementById("mes").value = "";
                }
                else $('#sp_mes').hide();
            };
    
    
        </script>
    }
    
    

    The Getting Started tutorials illustrate the fundamentals required to write MVC applications.

    https://www.asp.net/mvc

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 6, 2018 1:36 PM

All replies

  • User475983607 posted

    There are several logical and/or syntax errors in in the code.  Please use developer tools to debug the HTML source.

    For example...

    if(@ViewBag.Message!= null)
    { 
    

    if Message is not null, the JavaScript will render as...

    if(someVar != null)
    { 

    If Message is null then the code will throw an unexpected token error.

    The intent of the code is not clear so I'm not sure how to help you as null should be handled in the server not the client.

    The AJAX function is not correct and missing the URL which might be okay but there is no way to determine from the code. 

    You'll need to debug the code or provide a bit more information.

    Monday, August 6, 2018 11:48 AM
  • User134074001 posted

    Sir,

    The problem I am facing is that, I have already defined the function, still its giving function not defined.ACtually there used to be 1 function in a script, but now as I have added another one, the first one is not working and saying :-

    Uncaught ReferenceError: function is not defined

    what to do, how to use both the functions.

    Regards,

    Aniruddha

    Monday, August 6, 2018 11:57 AM
  • User753101303 posted

    Hi,

    You tried F12 Console when the page loads? Could ie be that you have a compile time error which prevents the correct compilation of this function ?

    Monday, August 6, 2018 12:24 PM
  • User-474980206 posted
    If there is a syntax error in a script block, none of the functions in th script block are defined. You have syntax errors.
    Monday, August 6, 2018 1:34 PM
  • User475983607 posted

    aniruddha4

    Sir,

    The problem I am facing is that, I have already defined the function, still its giving function not defined.ACtually there used to be 1 function in a script, but now as I have added another one, the first one is not working and saying :-

    Uncaught ReferenceError: function is not defined

    what to do, how to use both the functions.

    Regards,

    Aniruddha

    The browser is not loading the JavaScript code due to syntax errors.  The first step is to fix the syntax errors as clearly and openly recommended in my first post.  Reposting the same error will not magically change the outcome.   The code might work after fixing the syntax error.  But, you are using a layout page and the posted View has head and style elements.  Make sure you do not have the same function in another file. We cannot see that code and can only guess.

    The code is very difficult to understand due to the many syntax and logical errors.  We can only guess the actual intention.  Please debug your code or explain the intention.

    Below is my best guess.

    @{
        ViewBag.Title = "ContactUs";
    }
    
    <div class="container-fluid" style="width:100%">
        <header>Contact Us</header>
        <article style="float:left;width:50%">
            <strong>Address : </strong>
            <p>
                128, Parnarshree, Extn<br />
                Geeta Bhawan<br />
                Kolkata-700060,India<br />
            </p>
            <br> <strong>Email : </strong><p>aniruddha.mukhopadhyay4dec@gmail.com</p>
        </article>
        <div class="vl"></div>
        <article style="float:right;width:50%">
            <table style="background-color:aquamarine" align="center">
                <tr><td><span class="align-items-sm-end">Name: </span></td><td width="auto"><input type="text" id="name" /></td></tr>
                <tr><td><span class="align-items-sm-end">Email Id: </span></td><td><input type="text" id="email" /></td></tr>
                <tr><td><span class="align-items-sm-end">Subject: </span></td><td><input type="text" id="sub" /></td></tr>
                <tr><td><span class="align-items-sm-end">Message: </span></td><td><textarea id="mes"></textarea></td></tr>
                <tr><td><span class="align-items-sm-end"></span></td><td><input id="btnmes" type=submit onclick="return entr_mes()" /></td></tr>
            </table>
            <br />
            <span id="sp_mes" style="color:green">@ViewBag.Message</span>
    
        </article>
        <footer>
            <div class="section mcb-section  full-width  " style="padding-top:0px; padding-bottom:0px; background-color:beige">
                <div class="section_wrapper mcb-section-inner">
                    <div class="wrap mcb-wrap one  valign-top clearfix" style="">
                        <div class="mcb-wrap-inner">
                            <div class="column mcb-column one column_column ">
                                <div class="column_attr clearfix" style="">
                                    <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d921.4883191500388!2d88.29954022918274!3d22.505936138042305!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0x0!2zMjLCsDMwJzIxLjQiTiA4OMKwMTgnMDAuMyJF!5e0!3m2!1sen!2sin!4v1533298392373" width="1000" height="600" frameborder="0" style="border:0" allowfullscreen></iframe>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="section the_content no_content">
                <div class="section_wrapper">
                    <div class="the_content_wrapper"></div>
                </div>
            </div>
            <div class="section section-page-footer">
                <div class="section_wrapper clearfix"><div class="column one page-pager"></div></div>
            </div>
        </footer>
    </div>
    
    @section scripts{
        <script>
            function entr_mes() {
                var nm = document.getElementById("name").value;
                var em = document.getElementById("email").value;
                var sb = document.getElementById("sub").value;
                var ms = document.getElementById("mes").value;
                $.ajax({
                    type: "POST",
                    contenttype: "multipart/form-data",
                    url: "ContactUs",
                    data: { name: nm.toString(), email: em.toString(), sub: sb.toString(), message: ms.toString() }
                });
            };
            function spa_chk()
            {
                if('@ViewBag.Message' != '')
                {
                    $('#sp_mes').show();
                    document.getElementById("name").value = "";
                    document.getElementById("email").value = "";
                    document.getElementById("sub").value = "";
                    document.getElementById("mes").value = "";
                }
                else $('#sp_mes').hide();
            };
    
    
        </script>
    }
    
    

    The Getting Started tutorials illustrate the fundamentals required to write MVC applications.

    https://www.asp.net/mvc

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 6, 2018 1:36 PM