Help Creating JS Relative Path Function

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

    Help Creating JS Relative Path Function

    I'm trying to write a Javascript to set a relative path for my files, s
    I don't have to type in an absolute path. I know that I need to someho
    define the root directory, likely using /, then I need to have th
    function check to see where the file sits, then add the appropriat
    ../ infront of it.

    Unfortunatly I'm totally stumped as to what I need to do. A
    ex-co-worker of mine used to have a function similar to this, howeve
    we've lost touch.

    Anyone have an idea how this might work? It makes it so much easier t
    place items into webpages without having to hardcode the entire pat
    for everything.

    Regards,

    John William
    -
    b1azes

  • JimMenees

    #2
    Re: Help Creating JS Relative Path Function

    function getPath( ){
    var path = document.locati on;
    var str = new String(path);
    var len = str.length;
    var start = str.lastIndexOf ("/");
    var document_path = str.substring(s tart +1,len);
    alert(document_ path);
    }

    Comment

    • Mick White

      #3
      Re: Help Creating JS Relative Path Function

      b1azesk wrote:
      [color=blue]
      > I'm trying to write a Javascript to set a relative path for my files, so
      > I don't have to type in an absolute path. I know that I need to somehow
      > define the root directory, likely using /, then I need to have the
      > function check to see where the file sits, then add the appropriate
      > ./ infront of it.
      >
      > Unfortunatly I'm totally stumped as to what I need to do. An
      > ex-co-worker of mine used to have a function similar to this, however
      > we've lost touch.
      >
      > Anyone have an idea how this might work? It makes it so much easier to
      > place items into webpages without having to hardcode the entire path
      > for everything.
      >
      > Regards,
      >
      > John Williams
      > --
      > b1azesk[/color]
      [color=blue]
      >[/color]

      var loc=location.to String()
      var fileName = loc.substring(l oc.lastIndexOf( "/")+1)

      Mick

      Comment

      • Ivo

        #4
        Re: Help Creating JS Relative Path Function

        "b1azesk" wrote[color=blue]
        > I'm trying to write a Javascript to set a relative path for my files, so
        > I don't have to type in an absolute path. I know that I need to somehow
        > define the root directory, likely using /, then I need to have the
        > function check to see where the file sits, then add the appropriate
        > ./ infront of it.[/color]

        Did you know that, because it starts with a slash, <a href="/some/dir/">
        always points to same location, no matter how deep in your site you put this
        link? No javascript needed at all!

        Apart from that:

        document.locati on.pathname.spl it('/').length-2

        is a number representing the number of subdirectories. So

        var path='';
        for( var i=0; i<document.loca tion.pathname.s plit('/').length-2; i++)
        path+='../';

        will give you a string which can be used to back up to the root. Not sure if
        this is what you were looking for.
        HTH
        Ivo


        Comment

        Working...