The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Javascript display current directiory
tk=lm2408
post Feb 7 2017, 05:43 AM
Post #1


Newbie
*

Group: Members
Posts: 19
Joined: 31-January 17
Member No.: 26,289



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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Feb 7 2017, 07:03 AM
Post #2


Don't like donuts. Don't do MySpace.
********

Group: WDG Moderators
Posts: 17,624
Joined: 9-August 06
Member No.: 6



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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
tk=lm2408
post Feb 7 2017, 07:12 AM
Post #3


Newbie
*

Group: Members
Posts: 19
Joined: 31-January 17
Member No.: 26,289



That works great thank you very much Pandy:)
Btw is it possible to remove the slash between the directory names?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Feb 7 2017, 10:20 AM
Post #4


Don't like donuts. Don't do MySpace.
********

Group: WDG Moderators
Posts: 17,624
Joined: 9-August 06
Member No.: 6



Do you want to remove all slashes or only the last one?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
tk=lm2408
post Feb 8 2017, 12:05 AM
Post #5


Newbie
*

Group: Members
Posts: 19
Joined: 31-January 17
Member No.: 26,289



All of them if possible.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
tk=lm2408
post Feb 8 2017, 04:09 AM
Post #6


Newbie
*

Group: Members
Posts: 19
Joined: 31-January 17
Member No.: 26,289



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?

This post has been edited by tk=lm2408: Feb 8 2017, 04:25 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Feb 8 2017, 09:02 AM
Post #7


Don't like donuts. Don't do MySpace.
********

Group: WDG Moderators
Posts: 17,624
Joined: 9-August 06
Member No.: 6



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Feb 8 2017, 09:10 AM
Post #8


Don't like donuts. Don't do MySpace.
********

Group: WDG Moderators
Posts: 17,624
Joined: 9-August 06
Member No.: 6



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?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
tk=lm2408
post Feb 9 2017, 12:57 AM
Post #9


Newbie
*

Group: Members
Posts: 19
Joined: 31-January 17
Member No.: 26,289



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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Feb 9 2017, 07:56 AM
Post #10


Don't like donuts. Don't do MySpace.
********

Group: WDG Moderators
Posts: 17,624
Joined: 9-August 06
Member No.: 6



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
tk=lm2408
post Feb 11 2017, 01:52 AM
Post #11


Newbie
*

Group: Members
Posts: 19
Joined: 31-January 17
Member No.: 26,289



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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Feb 11 2017, 08:40 AM
Post #12


Don't like donuts. Don't do MySpace.
********

Group: WDG Moderators
Posts: 17,624
Joined: 9-August 06
Member No.: 6



What browser on the affected computer?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
tk=lm2408
post Feb 13 2017, 03:33 AM
Post #13


Newbie
*

Group: Members
Posts: 19
Joined: 31-January 17
Member No.: 26,289



Hi Pandy,

It's firefox 51.0.1 32bit on both machines.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Feb 13 2017, 04:00 AM
Post #14


Don't like donuts. Don't do MySpace.
********

Group: WDG Moderators
Posts: 17,624
Joined: 9-August 06
Member No.: 6



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?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
tk=lm2408
post Feb 16 2017, 12:31 AM
Post #15


Newbie
*

Group: Members
Posts: 19
Joined: 31-January 17
Member No.: 26,289



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Feb 16 2017, 01:07 AM
Post #16


Don't like donuts. Don't do MySpace.
********

Group: WDG Moderators
Posts: 17,624
Joined: 9-August 06
Member No.: 6



I guess we have to give up. Yeah, please do. These things drive me nuts. blush.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 24th July 2017 - 07:40 PM