16

Is there an absolute path while declaring the tag?

this will resolve if I have a aspx page in a folder (one level) script src="../Scripts/jquery-1.4.1.js" type="text/javascript">

this will resolve if I have a aspx page in a folder (two level) script src="../../Scripts/jquery-1.4.1.js" type="text/javascript">

this will resolve if I have a aspx page in the main root script src="Scripts/jquery-1.4.1.js" type="text/javascript">

Do i really need to create different version for each relative path?

1
  • Are you using MVC or WebForms? Commented Aug 18, 2010 at 5:08

7 Answers 7

28

You may want to use a relative path from the domain root instead:

<script src="/Scripts/jquery-1.4.1.js" type="text/javascript">
Sign up to request clarification or add additional context in comments.

2 Comments

this does not work.. still havig "object expected" on $(document)
@ronald-yoh: It should if Scripts is located in your domain root :) ... Note: If your Scripts folder is at http://your-domain.com/mysite/Scripts/, then you'd have to use src="/mysite/Scripts/jquery-1.4.1.js". However if your Scripts is at http://your-domain.com/Scripts/ then the example in my answer should do it.
11

For ASP.NET MVC use Url.Content("~/Scripts/jquery-1.4.1.js") in your view. The tilde makes your path relative to the application root, which could be a sub-folder if you're running as an IIS virtual application.

If it's WebForms, try Page.ResolveUrl() or VirtualPathUtility.ToAbsolute() in your page.

(As an aside, you might also want to consider loading jQuery from a CDN)

Comments

5

When referencing scripts and css files in webforms applications, use

"<%=ResolveUrl("~/path/file.ext") %>"

This is similar to "@Url.Content("~/path/file.ext")" in MVC and will replace ~ (application root) with application base path regardless of whether is it root application on server or in some virtual directory. If you use absolute path (/path.file.ext) it may work for you when your application is in root of web site, but when you move it into virtual directory it may stop resolving resources.

Comments

4

if you need jquery use can use always one absolute path to google cdn

http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js

a good topic : what is the different form relative vs absolute paths read in :

Absolute vs relative URLs

(Coincidence : me and @Daniel Vassallo Participants in this post)

2 Comments

Interesting coincidence :) ... +1 for suggesting the Google CDN for jQuery. However I guess this problem extends to other scripts for the OP, and jquery-1.4.1.js was chosen just as an example.
I have tens of different javascript files on the site so using "ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" will only fix one javascript file.
3

Code inserts such as "<%=ResolveUrl("~/path/file.ext") %>" do not seem to be an option if you are using Themes. If you use them, you get an exception.

Comments

1

I prefer using <base> tag and giving refrence as per that base tag

some thing like: http://www.w3schools.com/tags/tag_base.asp

1 Comment

I've been using w3schools for years and I'm hard-pressed to think of any errors I've found. I think their reliability is pretty good. They don't include the proprietary extensions that many browsers support, even when almost every browser supports some flavor of it, so they're incomplete in that sense. As to not being a branch of the W3C, did they ever say they were? I don't suppose that every web site that has "sql" in the name is affiliated with ANSI.
1
<script src="/Scripts/jquery-1.4.1.js" type="text/javascript">

This one does not work at all in web form. "/" does not represent website root dir.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.