PDA

View Full Version : Relativity of Paths?



phb5000
06-02-2007, 11:37 AM
Hi,

When dealing with paths in javascript/html, they are always relative to the location of the current page on which the code is executing, correct?

However, what if I want to make my paths relative to the location of the web root? Is this possible?

(I want to use this for both images and ajax http requests).

/phb5000

mburt
06-02-2007, 03:28 PM
If the "www" portion of the url is what you're looking for:

"http://"+window.location.hostname+"/"

jscheuer1
06-02-2007, 03:34 PM
That won't work in HTML. The proper method to make the path start at the root of the domain is:

/

placed at the start of the path.

mburt
06-02-2007, 03:38 PM
This also depends on the current directory, or the relative path. What I suggested was using an absolute path.

That won't work in HTML.
In what way won't that work in "HTML"?

jscheuer1
06-02-2007, 04:33 PM
This also depends on the current directory, or the relative path. What I suggested was using an absolute path.

That won't work in HTML.
In what way won't that work in "HTML"?



http://"+window.location.hostname+"/"

Requires javascript. Preceding the path with / means 'from the root' and doesn't depend upon the current directory.

mburt
06-02-2007, 05:35 PM
Yes, but using it in a variable then adding it dynamically would be no problem. Using / only works if you're running a page from the corresponding directory.

djr33
06-02-2007, 06:53 PM
John is correct.

<a href="/folder/file.ext"> would link to www.your.com/folder/file.ext, no matter where the current page exists.


EDIT: The only catch here is that it points to the root, not just 'www.your.com' always, so if that is set differently, it might point somewhere else. If you're on a subdomain, it may point to 'sub.your.com' instead, etc. This would depend on the server configuration. In general, though, it will point to the main part of your site, then you can navigate to any file/folder on the site.

thetestingsite
06-02-2007, 06:59 PM
Using / only works if you're running a page from the corresponding directory.

I think you are mistaking '/' and './'.

mburt
06-02-2007, 11:08 PM
So if you had a page in:
folder/folder1/folder2

<a href="/folder">..</a>
Would take you to "folder"?

thetestingsite
06-02-2007, 11:10 PM
So if you had a page in:
folder/folder1/folder2

<a href="/folder">..</a>
Would take you to folder?

Correct.