PDA

View Full Version : Help needed adding "sub space for underscore or %20" in script



mana7
05-13-2007, 10:33 PM
Hello.

I am using a script that I received from someone after I posted a request at this site (thanks!), which finds a specific directory name and displays it on the webpage in a specified table. The script is working well, except for directories with two or more words. If the multiple word directory name is written with an _underscore_ in place of the blank space, then the underscore is displayed in the title. If the directory has a blank space, then the blank space is displayed as "%20" in the title.

For example:

1) A directory named "Blue_Bird" becomes a section title displayed as "Blue_Bird" ... but I would like the script to be told to substitutle a blank space for an underscore, so the title would read: "Blue Bird"

2) A directory named "Red Bird" becomes a section title displayed as "Red%20Bird" ... but I would like the script to be told to display a blank space for a blank space (or for a "%20", if it is picking this up from the code), so the title would read: "Red Bird"

Following is a copy of the existing script:

<script type="text/javascript">
function disp_current_directory(){
var ext='htm';
var dirs=window.location.href.split('/'),
cdir=dirs[dirs.length-2];
document.write('<a href="'+cdir.toLowerCase()+'.'+ext+'">'+cdir.toUpperCase()+'<\/a>');
}
disp_current_directory();
</script>

Thanks in advance for any help!
Larry

djr33
05-14-2007, 12:59 AM
I believe str.replace should be able to work.

mana7
05-14-2007, 01:09 AM
Thanks GM,

I'm a novice.
Would it be possible for you to insert str.replace into the exisiting script and resend?

Thanks!
Larry

djr33
05-14-2007, 01:30 AM
I'm actually not sure how to use it, since JS isnt' really my area. I'd suggest googling it, or just being patient until someone else can tell you.

http://www.w3schools.com/jsref/jsref_replace.asp <<looks like good info.

So... something like:
mystring.str.replace('_',' ');

jscheuer1
05-14-2007, 03:59 AM
<script type="text/javascript">
function disp_current_directory(){
var ext='htm';
var dirs=window.location.href.split('/'),
cdir=dirs[dirs.length-2];
document.write('<a href="'+cdir.toLowerCase()+'.'+ext+'">'+unescape(cdir).toUpperCase().replace(/_/g,' ')+'<\/a>');
}
disp_current_directory();
</script>

djr33
05-14-2007, 04:31 AM
You'd need to use the function twice, though, since you want spaces kept as well. (Or, &#37;20.)

jscheuer1
05-14-2007, 06:51 AM
You'd need to use the function twice, though, since you want spaces kept as well. (Or, %20.)

Who, me? Which function? The way I wrote the function, it unescapes (which replaces all %## entities with their actual characters) before it replaces underscores with spaces. :p

One thing that puzzles me though, and I think I wrote the original code that this comes from is why .toLowerCase() for the link's href? I think it should be the verbatim cdir string because, if that contains upper case letters and they are changed to lower case letters, it will become a different and probably invalid href.

djr33
05-14-2007, 07:26 AM
Oh, ok. I misread your script. I agree about the lowercase.