Question

I have a simple javascript function

function SayTest() {
    alert("test");
}

I include the javascript file in my MVC 4 view

<script type="javascript" src="~/Scripts/MyScripts/MyScript.js"></script>

and call the SayTest function from within my view

<script type="text/javascript">
    $(document).ready(function () {
        $("#myBtn").click(function () {
            SayTest();
        });
    });
</script>

I'm getting an undefined function error. This is driving me nuts. If the js file was included and the function is define, why do I get an undefined function error?

Was it helpful?

Solution

Try using :

<script type="text/javascript" src="~/Scripts/MyScripts/MyScript.js"></script>

OTHER TIPS

You probably need to include jQuery into your View page:

<script type="text/javascript" src="~/Scripts/jquery.min.js"></script>

Make sure that's before any code that calls $().

(Note: You might have to change the exact filename to match what's in your project).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top