Relative paths in .js file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • epigram

    #1

    Relative paths in .js file

    I've got a javascript menu that uses an external .js file. In the .js file,
    there is a function that gets called by each of my .html pages to build up
    the menu for the page. The way I want to structure my site is to put my
    main index.html file in the root folder, and then have subdirectories for
    various logical sections of my site that will contain related .html files.
    I was hoping that the paths in the .js file would be relative to the
    directory the .js file was located in (as CSS files appear to do), but that
    doesn't appear to be the case. It appears the relative path is relative to
    the directory the html file is in.

    So, the problem is if I make the relative paths, in the .js file, work for
    the main index.html file, it doesn't work for any of the .html files in the
    subdirectories, or if I get to work for the files in the subdirectories, it
    doesn't work in the main index.html file. I'm just looking for a best
    practice way to solve this problem. One way I thought about doing it was to
    put a parameter into the menu-building function that would indicate if the
    calling html "file" was in the root directory or one of the subdirectories.
    I also realized that I could put the main index.html into a subfolder itself
    and that would solve the problem, but then I believe I'd have to put some
    default html file in my root and redirect to the "real" default page for the
    site. I'd like to avoid that option.

    Any ideas would be much appreciated.

    Thanks!


  • Maury

    #2
    Re: Relative paths in .js file

    epigram ha scritto:[color=blue]
    > One way I thought about doing it was to
    > put a parameter into the menu-building function that would indicate if the
    > calling html "file" was in the root directory or one of the subdirectories.[/color]

    I use this solution, too

    Bye

    Comment

    • Claudio Procida

      #3
      Re: Relative paths in .js file

      Il Mon, 27 Jun 2005 11:03:18 -0400, epigram ha scritto:
      [color=blue]
      > I've got a javascript menu that uses an external .js file.[/color]
      [color=blue]
      > So, the problem is if I make the relative paths, in the .js file, work for
      > the main index.html file, it doesn't work for any of the .html files in the
      > subdirectories, or if I get to work for the files in the subdirectories, it
      > doesn't work in the main index.html file. I'm just looking for a best
      > practice way to solve this problem.[/color]
      [color=blue]
      > Any ideas would be much appreciated.[/color]

      in the external .js file refer to location.pathna me and act consequently

      --
      Firefox, il browser sicuro, veloce, affidabile.
      <http://snipurl.com/ftpi>

      Comment

      • drWot

        #4
        Re: Relative paths in .js file

        If you reference all your src and href urls from the root (with a leading
        slash) they will link correctly.
        For instance:
        If you want to reference 'http://www.yourhost.co m/shared/somefile.gif' from
        the root, from the "/shared/" directory or from deep in some other
        directory, use:
        " src='/shared/somefile.gif' ".


        Comment

        Working...