Results 1 to 8 of 8

Thread: Help needed adding "sub space for underscore or %20" in script

  1. #1
    Join Date
    Jan 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help needed adding "sub space for underscore or %20" in script

    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

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I believe str.replace should be able to work.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jan 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks GM,

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

    Thanks!
    Larry

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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('_',' ');
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <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>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You'd need to use the function twice, though, since you want spaces kept as well. (Or, &#37;20.)
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by djr33 View Post
    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.

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Oh, ok. I misread your script. I agree about the lowercase.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •