../Msgboard/General customization/simple (and possibly stupid) javascript question ...
zick - 13 Nov. 2003 - 01:48:
simple (and possibly stupid) javascript question ...
i call it stupid because i'm sure there's a way to do what i want, only i don't know how ... and when someone enlightens me, i'll probably say "DOH!!!" ...
anyway, is there a way to keep forward slashes in a string in javascript ... i'll give an example and then you'll probably understand. in a folder.htt file you can use the %THISDIRPATH% to write the current working directory's path (ex. c:\windows\desktop) ... when you try to catch the path with javascript though (var path = "%THISDIRPATH%"

, it is returned minus the backslashes (ex. c:windowsdesktop). i would like to be able to keep those slashes in there, if possible.
are there any string manipulating functions in javascript that would allow me to do this?
thanks for any feedback.
What happens is windows replaces %THISDIRPATH% with the path before anything else happens, so in reality your file looks like this
var path = "c:\windows\desktop";
but backslashes are used to mark special characters (and \w doesn't exist for example), so the backslash gets removed.
So you can't use a function (there's no way to get back to the original path), but check this :
http://www.virtualplastic.net/html/wv_faq.html#scripting :
"Accessing special variables by scripting"
And another example:
<span id="path" style=" display: none">%THISDIRPATH%</span>
<script>
var path = document.all.path.innerHTML;
document.write(path);
</script>
14 Nov. 2003 - 14:06
craeonics
Hmm, I don't think the slashes are stripped until you actually do something with the variable where they get evaluated (like passing it to a function as an argument or writing it to the screen), so you could try something like
path=path.split('\\').join('\\\\')
...and see if that works. Haven't tested the above snippet, by the way, but it's how I do a search/replace in ActionScript (seeing it lacks something like str_replace()), so it should be valid JavaScript.
17 Nov. 2003 - 22:01
zick
ok, kmr, thanks that worked fine ...
one more real stupid thing, though.
i used the path with the split method to build an array of each folder in a directory, like so:
path.split("\\");
then i used a simple for loop to write out each folder on a separate line, and it looks something like this:
c:
windows
desktop
what i'd like to do is something like this (the dashes (-) represent spaces):
c:
-windows
--desktop
i just seem to get something to work right. now my javascript book is ancient and i probably should get another one, but is there some function that repeats a string (say like a space) so many number of times, something like:
repeat(" ", 6);
that would repeat a space 6 times.
anyone who can offer feedback would be much appreciated. thanks
18 Nov. 2003 - 06:22
saunders
I don't think there is.
Personally, I coded something like that on my own, I was using it to ensure numerical strings were a certain length, but with some simple tweaking:
function str_pad(strIn,padNum,padChar,side){
if(!padChar) padChar=" ";
strOut=strIn;
for(i=0;i<padNum;i++){
if(side=='r') strOut=padChar+strOut;
else strOut=strOut+padChar;
}
return strOut;
}
example:
paddedString=str_pad("Hello World","5","!","r");
paddedString becomes "Hello World!!!!!"
Edit: Er, I guess I went a bit overboard.. hehe.
Simpler function here:
function str_rep(c,n){
strOut='';
for(i=0;i<n;i++){
strOut+=c;
}
return strOut;
}
18 Nov. 2003 - 10:55
grigri
Don't forget that unless you're displaying this result in a <TEXTAREA> or a <PRE>, HTML won't acknowledge your extra spaces, it will only print one. To get more, don't use a space, use a non-breaking space ( ), use a TEXTAREA/PRE, or use
css attributes like margin-left.
Example:
/* Path components in path_array */
var spacer = '';
for (i=0; i<path_array.length; i++)
{
document.writeln(spacer + '<DIV>' + path_array[i] + '</DIV>');
spacer += ' ';
}
Example2:
/* Path components in path_array */
for (i=0; i<path_array.length; i++)
{
document.writeln('<DIV STYLE=\"border-left: ' + (i*16) +'px\">' + path_array[i] + '</DIV>');
}
18 Nov. 2003 - 23:31
zick
thanks grigri,
i ended up using your first example ...
and i can't believe i didn't think of adding strings.
Please log-in to post.
You need to be logged in to post. To log-in, or to register an account go -
there.
Options
../Msgboard/General customization/simple (and possibly stupid) javascript question ...