#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    65
    Rep Power
    13

    print current directory using javascript


    I am able print the path to where my file is using javascript. I can get it to return:

    /shoes/heels/sale.html
    or
    /shoes/tennis/neweststyles.html

    What I want is to be able to just print the current directory that the file is in
    (in the above examples it would be "heels" and "tennis" respectively).
    Note the directory and filenames are typically different lengths.

    This is the code I'm using:

    var dir = location.pathname.substring(0,location.pathname.lastIndexOf('/')+1);
    document.write("<center>" + dir + "</center>");

    Help?
  2. #2
  3. mod_dev_shed
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    14,817
    Rep Power
    1099
    Code:
    var path = document.location.pathname;
    var dir = path.substr(indexOf('/', 1), path.lastIndexOf('/')+1);
    # Jeremy

    Explain your problem instead of asking how to do what you decided was the solution.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    65
    Rep Power
    13
    Hmmm...well, that didn't print anything at all when I used it like this:

    var path = document.location.pathname;
    var dir = path.substr(indexOf('/', 1), path.lastIndexOf('/')+1);
    document.write("<center>" + dir + "</center>");
  6. #4
  7. mod_dev_shed
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    14,817
    Rep Power
    1099
    Sorry, forgot to give indexOf an object (JavaScript is a little different than PHP, which is what I'm used to) and I needed to use substring() instead of substr(), as they appear to be different.
    Code:
    var path = document.location.pathname;
    var dir = path.substring(path.indexOf('/', 1)+1, path.lastIndexOf('/'));
    # Jeremy

    Explain your problem instead of asking how to do what you decided was the solution.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    65
    Rep Power
    13
    Thanks! But... Now I get

    shoes/heels
    or
    shoes/tennis

    Any way to just get the "shoes" directory...OR just the "heels" or "tennis" subdirectories?

    I really appreciate this help! I spent 4 hours searching yesterday for a way to do this.
    Last edited by gennyblake; November 10th, 2002 at 12:56 PM.
  10. #6
  11. mod_dev_shed
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    14,817
    Rep Power
    1099
    Hmmm, it worked for me. I actually tested it before I posted this time, and it gave me the 'heels' or 'tennis' part. Maybe somebody else has an idea???
    # Jeremy

    Explain your problem instead of asking how to do what you decided was the solution.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    65
    Rep Power
    13
    I can try to play with it a bit more - if someone can explain what this part of the code is doing?

    (path.indexOf('/', 1)+1, path.lastIndexOf('/')

    thanks!
  14. #8
  15. mod_dev_shed
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    14,817
    Rep Power
    1099
    path.indexOf('/', 1)+1

    The index (number) of the first forward slash after the first character plus one.

    The offset (second parameter to indexOf) is so that it doesn't return the index of the first / at the beginning of the pathname. The plus one makes it so that the / isn't included.

    lastIndexOf('/')

    I think you get this one b/c you used it. But, the index (number) of the last occurrence of forward slash.

    Basically, it should give you everthing b/w the second / and the last /.
    # Jeremy

    Explain your problem instead of asking how to do what you decided was the solution.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    65
    Rep Power
    13
    path.indexOf('/', 1)+1
    The index (number) of the first forward slash after the first character plus one.
    Ok - I'm starting to understand this a bit more now. Question - is it possible to get the index of the second forward slash (and have that be my starting point instead of the first forward slash)? So what is extracted would be between the second and third forward slashes?

    Thanks again for all your help! I don't use javascript much (gee, can you tell?) but I have no choice for this particular project.
  18. #10
  19. mod_dev_shed
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2002
    Location
    Atlanta, GA
    Posts
    14,817
    Rep Power
    1099
    path.indexOf('/', 1) should give you the second / because it gives you the first / after the first character, and the first character is the first /. I put this code up [ dead link removed ] for you to take a look at. It's working for me in IE6. You can view the source, which has a little more than we've been working with so that I could document.write all the parts, but you can still see the code.
    Last edited by jharnois; July 20th, 2003 at 01:55 PM.
    # Jeremy

    Explain your problem instead of asking how to do what you decided was the solution.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    65
    Rep Power
    13
    awesome - thanks - I really appreciate your help!

    Genny

IMN logo majestic logo threadwatch logo seochat tools logo