<p>I'm working on a C#/ASP.NET project that has all the javascript files in a /Javascript folder. If I refer to the JS file using this syntax: src="/Javascript/jsfile.js" then the file is correctly picked up if the project is deployed to the root of the URL.</p>

<p>However, if this "web site" is deployed to a sub-folder of the main url this won't work. So the solution could be to use relative urls - but there's a problem with that as well because the master pages reference many of the javascript files and these master pages can be used by pages in the root and in subfolders many levels deep.</p>

<p>Does anybody have any ideas for resolving this?</p>

[{'answer_id': 43222, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 5, 'selected': True, 'text': '<p>If you reference the JS-file in a section that is "runat=server" you could write src="~/Javascript/jsfile.js" and it will always work.</p>\n\n<p>You could also do this in your Page_Load (In your masterpage):</p>\n\n<pre><code>Page.ClientScript.RegisterClientScriptInclude("myJsFile", Page.ResolveClientUrl("~/Javascript/jsfile.js"))\n</code></pre>\n'}, {'answer_id': 43252, 'author': 'Jared', 'author_id': 3442, 'author_profile': 'https://Stackoverflow.com/users/3442', 'pm_score': 2, 'selected': False, 'text': '<p>Try something like this in the Master Page:</p>\n\n<pre><code>&lt;script type="text/javascript" src="&lt;%= Response.ApplyAppPathModifier("~/javascript/globaljs.aspx") %&gt;"&gt;&lt;/script&gt;\n</code></pre>\n\n<p>For whatever reason, I\'ve found the browsers to be quite finicky about the final  tag, so just ending the tag with /> doesn\'t seem to work.</p>\n'}, {'answer_id': 43287, 'author': 'erlando', 'author_id': 4192, 'author_profile': 'https://Stackoverflow.com/users/4192', 'pm_score': 0, 'selected': False, 'text': "<p>@Jared: IE needs that /script . FF doesn't care.</p>\n"}, {'answer_id': 43734, 'author': 'Brad Tutterow', 'author_id': 308, 'author_profile': 'https://Stackoverflow.com/users/308', 'pm_score': 2, 'selected': False, 'text': '<p>The brand-new version of ASP.NET (<a href="http://msdn.microsoft.com/en-us/vstudio/products/cc533447.aspx" rel="nofollow noreferrer">3.5 SP1</a>) has a nifty feature called <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.compositescriptreference.aspx" rel="nofollow noreferrer">CompositeScript</a>.  This allows you to use a <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.scriptreference.aspx" rel="nofollow noreferrer">ScriptManager</a> to reference lots of tiny little .js files server-side and have them delivered as a single .js file to the client.  </p>\n\n<p>Good for the client since it only has to download one file.  Good for you since you can maintain the files however you want on the server side.</p>\n\n<pre><code>&lt;asp:ScriptManager ID="ScriptManager1" \n    EnablePartialRendering="True"\n    runat="server"&gt;\n    &lt;Scripts&gt;\n        &lt;asp:ScriptReference \n            Assembly="SampleControl" \n            Name="SampleControl.UpdatePanelAnimation.js" /&gt;\n    &lt;/Scripts&gt;\n&lt;/asp:ScriptManager&gt;\n</code></pre>\n'}, {'answer_id': 262558, 'author': 'Chris Zwiryk', 'author_id': 734, 'author_profile': 'https://Stackoverflow.com/users/734', 'pm_score': 0, 'selected': False, 'text': '<p>You might want to take a look at <a href="http://www.codeproject.com/KB/aspnet/UsingTheFileResolver.aspx" rel="nofollow noreferrer">FileResolver</a>. It\'s an HTTP Handler that allows you to do this:</p>\n\n<pre><code>&lt;link rel="stylesheet" href="~/resources/stylesheet.css.ashx" /&gt;\n</code></pre>\n\n<p>And have the tilde (as well as any tildes within the file) expanded properly.</p>\n'}]
['https://Stackoverflow.com/questions/43218', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1463/']