Help - Search - Members - Calendar
Full Version: Javascript display current directiory
HTMLHelp Forums > Programming > Client-side Scripting
tk=lm2408
Hi,

I'm trying to list the current directory that the current page is located in.
I have it working except it displays %20 instead of a space in the directory name.
The code I have is:
CODE

var path = document.location.pathname;
var dir = path.substring(path.indexOf('DIRLISTTEST/', 1),
path.lastIndexOf('/'));
document.write(dir);

Can anyone help?
Thank you for your time
Greg
pandy
Add decodeURIComponent() . For example here.

CODE
document.write(decodeURIComponent(dir));


decodeURI() also works, but I think decodeURIComponent() is the proper one to use here.

https://developer.mozilla.org/en-US/docs/We...jects/decodeURI
https://developer.mozilla.org/en-US/docs/We...odeURIComponent


FYI there are also corresponding encode functions, should you need them. wink.gif
tk=lm2408
That works great thank you very much Pandy:)
Btw is it possible to remove the slash between the directory names?
pandy
Do you want to remove all slashes or only the last one?
tk=lm2408
All of them if possible.
tk=lm2408
The other problem I have is it doesn't seem to follow the uppercase/lowercase letters in the parent directory names, Eg. a folder named Foo gets listed as foo in all lowercase.

So If I have a path like:
C:/Foo/Footwo

it displays as:
c:/foo/Footwo

If I have:
C:/Foo/Footwo/Foothree

it displays
c:/foo/Footwo/Foothree

So the first directory shows up as lowercase and the rest of the directories shows up correctly.

Is there an easy way to fix that?
pandy
QUOTE(tk=lm2408 @ Feb 8 2017, 06:05 AM) *

All of them if possible.



Good, because that's much easier.

You can use string replace(). I don't usually link to w3schools, but it was the most understandable page I found now.
http://www.w3schools.com/jsref/jsref_replace.asp

CODE

document.write(decodeURIComponent(dir).replace(/\//g, 'X'));


I used X as the replacement character. You can use what you want or nothing within the quotes. As you can read at the linked to page the global flag can only be used with regex, hence the first parameter looks as it does. If it had been a normal letter, for example 'a', that should be replaced it would have looked like this.
CODE
replace(/a/g, 'X')

But the slash needs to be escaped with a backslash. Just substituting the first instance of a word or letter is really straightforward, no regex needed.
pandy
QUOTE(tk=lm2408 @ Feb 8 2017, 10:09 AM) *

The other problem I have is it doesn't seem to follow the uppercase/lowercase letters in the parent directory names, Eg. a folder named Foo gets listed as foo in all lowercase.

So If I have a path like:
C:/Foo/Footwo

it displays as:
c:/foo/Footwo

If I have:
C:/Foo/Footwo/Foothree

it displays
c:/foo/Footwo/Foothree

So the first directory shows up as lowercase and the rest of the directories shows up correctly.

Is there an easy way to fix that?


What? Do you mean the directory is literally named Foo, that the word Foo is hexed somehow? Or that the first directory name always gets converted to lower case? Both sounds crazy. Is there more to your script than you have shown here? A little toLowerCase() somewhere maybe?
tk=lm2408
Hi Pandy,

Thank you again for your help. I really appreciate your time and knowledge. The string replace works great.

As for Foo I used that as an example as all Directories give the same result, but I tried it on another computer and it works the way I want, so I think it's something screwy at my end. I'm gonna have a play with it now and see if I can figure out what is going on.

Again thank you so much for your help
Greg
pandy
In your examples it was just the first directory that was converted to lower case. Was that a mistake and it's all directories?

Strange anyway. Since case doesn't matter on Windows it isn't totally wacko. But I've always used Windows myself, and I don't think I've encountered this. Does the same thing happen if you bring up a command prompt and do a DIR for example or in Windows Explorer? Just curious.
tk=lm2408
Hi Pandy,

It is just the first directory that gets listed in lowercase. The rest are fine. I can't work it out. It does this on one pc and if I copy the same code to another pc it works fine??

The first letter in the directory name is upper case from explorer and from command promt
pandy
What browser on the affected computer?
tk=lm2408
Hi Pandy,

It's firefox 51.0.1 32bit on both machines.
pandy
I was one version behind but upgraded to the same version as you have. Doesn't happen for me. Does it happen with other browsers too on that machine?
tk=lm2408
Yeah I just tried IE on that machine and the same thing happens. Haven't really had much time lately to find out what is going on. If I find an answer I'll be sure to post it here.
pandy
I guess we have to give up. Yeah, please do. These things drive me nuts. blush.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2024 Invision Power Services, Inc.