Question

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?

Was it helpful?

Solution

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

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

OTHER TIPS

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)

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.

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)

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.

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

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

<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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top