Using JQuery in a Subfolder When the MasterPage is in the Root Folder
Asked Answered
A

2

5

I am trying to use the jquery library in ASP.NET in a subfolder called "samples" with a masterpage that is located in the root directory. Presently the references to the jquery scripts are located in the head tag of the master page. If the page I am creating is also in the root directory, everything works fine. If I move the page to the "samples" subdirectory, the jquery breaks.

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

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

...but then I lose the ability to use jquery intellisense, because I am no longer directly connected to the file in design time.

So my quesiton is this: How can I use the jquery library on a .aspx page without losing connectivity to the intellisense when my page is in a subfolder and the master page is in the root?

Anonymous answered 22/6, 2009 at 7:38 Comment(0)
L
10

simply use this:

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

putting / before js do the trick. I always keep my css and javascript files in separate folders and use this tweak to rightly include them.

for intellisense you can try this trick:

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

I'm not able to recall the source of this trick.

Loraine answered 22/6, 2009 at 7:42 Comment(5)
I do the same, a JavaScript and a Css folder directly of the root and then use a server relative URI to point to my CSS / JS files.Wilscam
I have verified that the / before js does indeed allow the code to run in both folders (so it's a better solution than the ResolveUrl), but it still will not allow me to see intellisense. The intellisense trick you listed does not work either.Anonymous
I take it back. When I add <script src="/js/jquery.js" type="text/javascript"></script> followed by <%if(true){%> <script src="/js/jquery.js" type="text/javascript"></script> <%}%> Then it works in both the root and the subfolder, and intellisense works in both places. Good enough for now. Thank you.Anonymous
Sorry, in the second line I meant to point out that the / should be removed, like this: <%if(true){%> <script src="js/jquery.js" type="text/javascript"></script> <%}%>Anonymous
This worked for me as well! The only thing I would note is i had to use <%if(false){%> since I didn't want to include the file twice...Nissie
M
1

You could use a script manager to include the JS files:

<asp:ScriptManager runat="server">
    <Scripts>
        <asp:ScriptReference Path="~/js/jquery.js" />
    </Scripts>
</asp:ScriptManager>
Maryettamaryjane answered 22/6, 2009 at 7:47 Comment(2)
This works for both folders, but doesn't connect me to the intellisense.Anonymous
very useful trick, i saw it elsewhere, but couldn't find it. +1Renewal

© 2022 - 2024 — McMap. All rights reserved.