views:

293

answers:

5

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?

+4  A: 

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
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.
Daniel Vassallo
+3  A: 

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 :

http://stackoverflow.com/questions/2005079/absolute-vs-relative-urls

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

Haim Evgi
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
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.
A: 

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
@downvoter: plz write comment why its in appropriate
KoolKabin
A: 

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)

cxfx
not looking a code for server side.. it will be declarative
A: 
<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