Results 1 to 10 of 10

Thread: Relativity of Paths?

  1. #1
    Join Date
    Apr 2007
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Relativity of Paths?

    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

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    If the "www" portion of the url is what you're looking for:
    Code:
    "http://"+window.location.hostname+"/"
    - Mike

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

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

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    This also depends on the current directory, or the relative path. What I suggested was using an absolute path.
    Code:
    That won't work in HTML.
    In what way won't that work in "HTML"?
    - Mike

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by mburt View Post
    This also depends on the current directory, or the relative path. What I suggested was using an absolute path.
    Code:
    That won't work in HTML.
    In what way won't that work in "HTML"?
    Code:
    http://"+window.location.hostname+"/"
    Requires javascript. Preceding the path with / means 'from the root' and doesn't depend upon the current directory.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

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

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Last edited by djr33; 06-02-2007 at 07:05 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Quote Originally Posted by mburt View Post
    Using / only works if you're running a page from the corresponding directory.
    I think you are mistaking '/' and './'.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  9. #9
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    So if you had a page in:
    folder/folder1/folder2
    Code:
    <a href="/folder">..</a>
    Would take you to "folder"?
    - Mike

  10. #10
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Quote Originally Posted by mburt View Post
    So if you had a page in:
    folder/folder1/folder2
    Code:
    <a href="/folder">..</a>
    Would take you to folder?
    Correct.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •