Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Client-side Scripting _ Javascript display current directiory

Posted by: tk=lm2408 Feb 7 2017, 05:43 AM

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

Posted by: pandy Feb 7 2017, 07:03 AM

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/Web/JavaScript/Reference/Global_Objects/decodeURI
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent


FYI there are also corresponding encode functions, should you need them. wink.gif

Posted by: tk=lm2408 Feb 7 2017, 07:12 AM

That works great thank you very much Pandy:)
Btw is it possible to remove the slash between the directory names?

Posted by: pandy Feb 7 2017, 10:20 AM

Do you want to remove all slashes or only the last one?

Posted by: tk=lm2408 Feb 8 2017, 12:05 AM

All of them if possible.

Posted by: tk=lm2408 Feb 8 2017, 04: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?

Posted by: pandy Feb 8 2017, 09:02 AM

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.

Posted by: pandy Feb 8 2017, 09:10 AM

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?

Posted by: tk=lm2408 Feb 9 2017, 12:57 AM

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

Posted by: pandy Feb 9 2017, 07:56 AM

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.

Posted by: tk=lm2408 Feb 11 2017, 01:52 AM

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

Posted by: pandy Feb 11 2017, 08:40 AM

What browser on the affected computer?

Posted by: tk=lm2408 Feb 13 2017, 03:33 AM

Hi Pandy,

It's firefox 51.0.1 32bit on both machines.

Posted by: pandy Feb 13 2017, 04:00 AM

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?

Posted by: tk=lm2408 Feb 16 2017, 12:31 AM

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.

Posted by: pandy Feb 16 2017, 01:07 AM

I guess we have to give up. Yeah, please do. These things drive me nuts. blush.gif

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)