PDA

View Full Version : Directory structure breakdown

jimbowvu80s
11-17-2009, 02:43 PM
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>

jscheuer1
11-17-2009, 03:22 PM
<script type="text/javascript">
/([^\/]+\/[^\/]+)$/.test(location.href.replace(location.search, '').replace(/#/, '')); document.write('You are here: ' + RegExp.$1);
</script>

jimbowvu80s
11-17-2009, 03:46 PM
That works great John. Can you tell me how to remove the file name and only display the directory?

Thank you.

jscheuer1
11-17-2009, 04:04 PM
I wasn't sure if you wanted it that way or the way I did it. Getting rid of the filename is easy:

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

jimbowvu80s
11-17-2009, 04:37 PM
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).

jscheuer1
11-17-2009, 05:32 PM
I just realized that (since the filename is no longer required) there is a more efficient and less subject to error method:

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

jimbowvu80s
11-17-2009, 05:48 PM
This one doesn't display the directory.

jscheuer1
11-17-2009, 05:59 PM
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.

jimbowvu80s
11-17-2009, 08:51 PM
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.

jscheuer1
11-17-2009, 09:14 PM
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.