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?

Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
user384080
  • 4,576
  • 15
  • 64
  • 96

7 Answers7

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">
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
  • 1
    this does not work.. still havig "object expected" on $(document) – user384080 Aug 18 '10 at 05:48
  • 2
    @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. – Daniel Vassallo Aug 18 '10 at 05:56
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)

Chris Fulstow
  • 41,170
  • 10
  • 86
  • 110
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.

Goran Obradovic
  • 8,951
  • 9
  • 50
  • 79
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)

Community
  • 1
  • 1
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
  • 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. – Daniel Vassallo Aug 18 '10 at 05:23
  • I have tens of different javascript files on the site so using "http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" will only fix one javascript file. – user384080 Aug 18 '10 at 05:49
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.

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

KoolKabin
  • 17,157
  • 35
  • 107
  • 145
  • 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. – Jay Jun 11 '14 at 18:05
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.

John Li
  • 63
  • 5