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?

gene
  • 2,098
  • 7
  • 40
  • 98

2 Answers2

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

JonH
  • 32,732
  • 12
  • 87
  • 145
  • That worked. But why it did not work with `ResolveUrl`? – gene Feb 03 '16 at 20:00
  • @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. – JonH Feb 03 '16 at 20:04
  • I wonder if `~` won't get browser confused, will it? Would browser resolve the relative url? – Artyom Feb 03 '16 at 20:12
  • @Artyom - I think no confusion on behalf of the browser. – JonH Feb 03 '16 at 20:14
  • @JonH Though it looks like browser would be confused (also see [this](http://www.cs.tut.fi/~jkorpela/tilde.html) ). The thing is Asp.net resolves it behind the scenes (see [this](http://stackoverflow.com/questions/4563235/where-does-asp-net-virtual-path-resolve-the-tilde) ) – Artyom Feb 03 '16 at 20:30
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!

Artyom
  • 3,507
  • 2
  • 34
  • 67