80

I'm trying to add a script reference to jQuery in my master page so that it will work for any page. It currently looks like this

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

The problem is that the path is always relative to the executing aspx page so this will only work if the "jquery.js" file is located in the same folder. To make it work I have to change the line to:

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

This is obviously less than ideal because it will only work for pages that are two levels deep from the root folder. If I try the following, IIS throws an error about an unexpected character.

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

Any ideas?

EDIT: I forgot to mention as well that the script MUST be in the head tag

The current top answer throws a "ASP.NET Ajax client-side framework failed to load." error when I add it to my master page. Its thrown from javascript and not the .Net compiler. If I move the ScriptManager to the head section where it should be I get a compile error about the ScriptManager needing to be inside a form tag.

The third answer throws a "Illegal characters in path." exception from the compiler

EDIT 2: When I add that line to my head tag I get this error from IIS.

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)

SOLVED: I took the edited response from the answer below and put it inside an asp:ContentPlaceHolder element

TheDude
  • 1,182
  • 3
  • 10
  • 14
  • The solution for this problem https://learn.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-getting-started/master-pages/urls-in-master-pages-cs – Pranesh Janarthanan Apr 08 '20 at 14:16

10 Answers10

111

You could use a ScriptManager:

<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Scripts>
        <asp:ScriptReference Path="~/jquery.js" />
    </Scripts>
</asp:ScriptManager>

EDIT: If you absolutely need this in your <head> section, you could do something like:

<head>
    <script type="text/javascript" 
        src="<%= Page.ResolveClientUrl("~/jquery.js") %>"></script>
</head>

EDIT 2: According to the comments, if you are observing that

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)

you may need to change the above to use the data-binding syntax:

<head>
    <script type="text/javascript" 
        src="<%# Page.ResolveClientUrl("~/jquery.js") %>"></script>
</head>
Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
  • Aye, this is a highly recommended method in a lot of ASP.NET books I've written when dealing with paths in masterpages. – Kieran Senior Mar 30 '09 at 15:57
  • FYI, my co-worker and I just tried this out. This doesn't seem to work in a nested master page scenario *in the parent master page*. Moving it to the child master page, however, did the trick. – technomalogical Aug 11 '09 at 20:13
  • 3
    This worked for me on nearly all of my pages, but for 2 pages that would blow up with a certain exception: "The controls collection cannot be modified because the control contains code". The solution is to change the script include in the header from a response.write codeblock to a databind evaluator (ie - change <%= to <%# ). I would hazard a guess that it's some type of attempt on MS's part to stop http header attacks. A better explanation of this easy fix can be found at: http://leedumond.com/blog/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blocks/ – Jagd Apr 18 '13 at 17:23
  • @Jagd http://leedumond.com/blog/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blocks/ not found – PreguntonCojoneroCabrón Sep 07 '17 at 08:12
  • ***ScriptReference*** not add script in `head section` ? – Kiquenet Sep 09 '17 at 07:33
  • Use ***PlaceHolder*** https://stackoverflow.com/questions/778952/the-controls-collection-cannot-be-modified-because-the-control-contains-code-bl – Kiquenet Sep 09 '17 at 07:39
27

Try <%# instead of <%= in Master page under head section

<script type="text/javascript" 
        src="<%# ResolveUrl("~/YourScriptFolder/YourJQueryOrJavascript.js") %>">
</script>

Then in Code Behind of Master page under Page_Load Event

Page.Header.DataBind();

Now you are good to go with either jQuery and JavaScript as well as CSS just you need to change your path in ResolveUrl which file you want to handle CSS, JavaScript, jQuery.

potashin
  • 44,205
  • 11
  • 83
  • 107
Siddiq Baig
  • 586
  • 2
  • 17
  • 37
12

If you're not going to us asp:ScriptManager or absolute paths then you can do it like this:

<script runat="server" type="text/javascript" 
  src='<%= Page.ResolveUrl("~/jquery.js") %>'></script>
Keltex
  • 26,220
  • 11
  • 79
  • 111
3

I do not know whether you guys found the solution to your problem or not. I was facing the same problem and going nuts to figure out why do I get "jQuery is undefined" error on the plugins i use. I tried all the solutions i get from the internet but no luck at all.

But, suddenly something splash on my mind that may be the script files should be in order. So, I moved the jquery referece to first position and everything start working like charm.

Remember guys, if you're using any plugins with jquery, make sure you use the folloing order of setting reference to those fiels.

  1. reference to the jquery library
  2. reference to the other subsequent plug-in libraries and so on...

e.g.:

  1. "script src="js/jquery-1.3.2.min.js" type="text/javascript"...
  2. "script src="js/jqDnR.min.js" type="text/javascript"...
  3. "script src="js/jquery.jqpopup.min.js" type="text/javascript"...
  4. "script src="js/jquery.bgiframe.min.js" type="text/javascript"...

Always make sure you must put the jquery reference to first and then the subsequent libraries.

Hope, this solves your problem especially when you use with MasterPages. Its very strange that it works no matter what order you use when you don't use MasterPages but when you do, then it somehow requres the proper order.

Good luck and happy coding,

Vincent D'Souza

Jacob
  • 77,566
  • 24
  • 149
  • 228
3

Look at How to Run a Root “/”. This should fix all your issues regarding unresolved .js file paths. You basically reconfigure the VS Dev server to run your application as localhost:port/ as opposed to the regular localhost:port/application name/ making name resolution work the same way as it does on IIS.

agf
  • 171,228
  • 44
  • 289
  • 238
Rahul Sud
  • 31
  • 1
1

For absolute path of the file for any page use it following:

<script type="text/javascript" src="<%= Page.ResolveClientUrl("~/jquery.js") %>"></script> 
Umesh Bagalur
  • 311
  • 3
  • 11
0
<body>
<script language="javascript" src='<%= this.ResolveClientUrl("~/full/path/to/jquery.js") %>' type="text/javascript"></script>
</body>
Yusan Susandi
  • 229
  • 2
  • 7
  • 21
0
<script type="text/javascript" src="/full/path/to/jquery.js"></script>
karim79
  • 339,989
  • 67
  • 413
  • 406
  • 2
    Problem with paths off the root is that sometimes you may dev in a virtual dir and then deploy to the root of a site, or vice versa. – StuartLC Mar 11 '12 at 06:18
0

If this script tag goes directly to the browser, then you unlikely can substitute your site's root there. At least not on the server. So you can:

  1. Deploy site to the root of domain name and use absolute paths (simplest solution).
  2. Insert this link with server control.
  3. Preprocess resulting HTML before sending it to the client (with HttpResponse.Filter).
XOR
  • 2,177
  • 1
  • 17
  • 15
0

You can also use <base> HTML tag:

<base href="http://www.domain.com"></base>  

and then all the links in header section are relative to base address:

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

It's often useful when you have multiple publishing destinations, like local dev web server, demo server, etc. You just replace that base URL.

Hrvoje Hudo
  • 8,994
  • 5
  • 33
  • 42