Using jQuery in a subfolder When MasterPage is in the root folder

I am trying to use the jquery library in ASP.NET in a subfolder called "samples" with the main page located in the root directory. Links to jquery scripts are currently located in the main tag of the main page. If the page I create is also in the root directory, everything works fine. If I move the page to the "samples" subdirectory, jquery breaks.

I can fix the problem using something like the following in the main tag:

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

... but then I lose the ability to use jquery intellisense, because I no longer connect directly to the file during development.

So my quesiton is this: how can I use the jquery library on the .aspx page without losing touch with intellisense when my page is in a subfolder and the main page is in the root?

+5
source share
2 answers

just use this:

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

put / before js does the trick. I always keep css and javascript files in separate folders and use this setting to enable them correctly.

for intellisense you can try this trick:

<%if(true){%>
    <script src="/js/jquery.js" type="text/javascript"></script>
<%}%>

I can’t remember the source of this trick.

+10
source

You can use the script manager to include JS files:

<asp:ScriptManager runat="server">
    <Scripts>
        <asp:ScriptReference Path="~/js/jquery.js" />
    </Scripts>
</asp:ScriptManager>
+1
source

All Articles