Results 1 to 10 of 10

Thread: Directory structure breakdown

  1. #1
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Directory structure breakdown

    I found this script on IRT.org and was wondering if someone could give me a had at describing the different parts of it. It works fine, but I want to to tweak it. I want to only display the directory that the file resides in and not the entire path - for example - the script below gives me c:\programs\microsoft\mail\text.html. What I would like to only show is the "mail" directory.


    <script language="JavaScript"><!--
    thePath = '';
    loc = '' +location.href;
    paths = loc.substring(7).split('/');
    file = loc.substring(0,6);
    for (i=0, n=paths.length;i<n;i++) {
    file += "/" + paths[i];
    thePath += '> <a href="' + file + '">' + paths[i] + '<\/a> ';
    }
    document.write('You are here: ' + thePath.substring(1));
    //--></script>

  2. #2
    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

    Code:
    <script type="text/javascript">
    /([^\/]+\/[^\/]+)$/.test(location.href.replace(location.search, '').replace(/#/, ''));
    document.write('You are here: ' + RegExp.$1);
    </script>
    - John
    ________________________

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

  3. #3
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    That works great John. Can you tell me how to remove the file name and only display the directory?

    Thank you.

  4. #4
    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

    I wasn't sure if you wanted it that way or the way I did it. Getting rid of the filename is easy:

    Code:
    <script type="text/javascript">
    /\/([^\/]+)\/[^\/]*$/.test(location.href.replace(location.search, '').replace(/#/, ''));
    document.write('You are here: ' + RegExp.$1);
    </script>
    - John
    ________________________

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

  5. #5
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Thank you - works perfectly!

    I initally was trying to pass information from one frame to another, but I can't get it figured out (I have a post on the forum about this - Passing Data/Variables between frames).

  6. #6
    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

    I just realized that (since the filename is no longer required) there is a more efficient and less subject to error method:

    Code:
    <script type="text/javascript">
    /\/([^\/]+)\/[^\/]*$/.test(location.pathname);
    document.write('You are here: ' + RegExp.$1);
    </script>
    - John
    ________________________

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

  7. #7
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    This one doesn't display the directory.

  8. #8
    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

    Oh well, it does here. And it should in any browser. Maybe you copied it wrong. What browser are you using?

    However, it possible that some quirk of your setup or something else perhaps that I'm not aware of is preventing it from working as expected for you.

    But the location.pathname is the location.href stripped of the # (which in my earlier version probably should have been location.hash, but that brings up some cross browser issues) and search. I didn't change anything else, so it should work, and does here in several browsers.
    - John
    ________________________

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

  9. #9
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    I'm using IE 6.0xxx and it doesn't work, but I have another computer with IE 8.0xxx and it works.

    Thanks for your help.

  10. #10
    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

    Ah, I see. On a local machine, IE 6 gives the pathname incorrectly as, for example:

    \whatever\mail\text.htm

    But if the page is live, it will do it correctly, ex:

    /whatever/mail/text.htm

    Unless this is to be used for local work and not a live page, or for a combination of both, the pathname version is to be preferred as it will not be tripped up by query strings and/or hash markers that may trail the URL. Generally the first version will take care of those properly, but some unusual queries and/or hashes might mess it up. The location.pathname automatically strips queries and hashes.
    - John
    ________________________

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

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
  •