4

I have created an ASP.NET application and want to validate form with jQuery.

When refreshing the page, chrome's console shows the following error:

Uncaught ReferenceError: $ is not defined

This is my Master page code:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
    <title></title>

    <link href="Styles/bootstrap.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src='<% ResolveUrl ("~/scripts/jquery-1.11.3.js"); %>'></script>
    <script src="Scripts/validations.js" type="text/javascript"></script>
    <asp:ContentPlaceHolder ID="HeadContent" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form runat="server" class="form-inline" role="form">
        <div class="container">
            <div class="jumbotron">    
                <h1>Statement Generator</h1>
                <p>This application is used to generate PDFs</p>
            </div>
            <div class="row">
                <div class="col-xs-12">
                    <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
                </div>
            </div>
            <div class="clear">
            </div>
        </div>
        <div class="footer">

        </div>
    </form>
    <script type="text/javascript" src='<% ResolveUrl ("~/scripts/jquery-1.11.3.js"); %>'></script>
    <script type="text/javascript" src='<% ResolveUrl ("~/scripts/bootstrap.min.js"); %>'></script>

</body>
</html>

This is my code in validations.js:

$(function () {
    alert();
});

Am my missing something?

6
  • You are including jquery twice... try removing it from header Commented Feb 3, 2016 at 19:54
  • But should not I define jQuery before using it? Commented Feb 3, 2016 at 19:55
  • You need to remove jquery from the bottom. Commented Feb 3, 2016 at 19:56
  • Why do you include jquery 2 times? Check in console if $ is available, maybe bad path. Commented Feb 3, 2016 at 19:56
  • Jquery must not be loading on that page, why aren't you just linking directly to it, what's with the ResolveUrl? Commented Feb 3, 2016 at 19:56

2 Answers 2

4

You need to remove the last declaration of jquery, you've already got it declared inside of the <head> tag. Can you first try to remove the last instance and then try, another thing you can do is directly change it to:

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

Just to see if ResolveUrl() is the issue.

I think for scripts, its not necessary to use ResolveUrl, you should use ResolveUrl for things like UserControls or images.

Here's the msdn: https://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl.aspx

Sign up to request clarification or add additional context in comments.

5 Comments

That worked. But why it did not work with ResolveUrl?
@gene - See my edit, you can use ResolveUrl but you need the correct syntax, see the other answer by Artyom. But in this case your scripts should be sitting nicely in there own folder, something like ~/scripts/... My suggestion is it causes more confusion than what its worth.
I wonder if ~ won't get browser confused, will it? Would browser resolve the relative url?
@Artyom - I think no confusion on behalf of the browser.
@JonH Though it looks like browser would be confused (also see this ). The thing is Asp.net resolves it behind the scenes (see this )
1

Replace

<script type="text/javascript" src='<% ResolveUrl ("~/scripts/jquery-1.11.3.js"); %>'></script>

with

<script type="text/javascript" src='<%= ResolveUrl("~/scripts/jquery-1.11.3.js") %>'></script>

The reason it works <%= %> makes Response.Write while <% %> is a call to ResolveUrl without outputting it. See more at https://support.microsoft.com/en-us/kb/976112 Hope it helps!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.