Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing special characters "\"

Status
Not open for further replies.

rubychyld

Programmer
Mar 22, 2001
76
US
I am losing my "\" character in a string. How do I put them back in?

I am passing an asp variable into a javascript function. The variable is a url with value = \\myserver\
The problem is that javascript is removing the "\" because it is a "special" character.

Any idea how I can deal with this so the url gets written out exactly as it was passed in.

**I'm building dynamic link with asp, and passing it to a js function like this *****************************
Response.Write (&quot;<img src=&quot;&quot;library/icons/dir_belllogo.gif&quot;&quot; alt=&quot;&quot;edit&quot;&quot; onclick=&quot;&quot;javascript:showFileProperties(' &quot;& objFile.Name &&quot; ')&quot;&quot;>&quot;)
*****************************

the variable or js parameter is actually a file name that looks like this
\\qaserver\intranet\library\forms\agent.pdf

When it gets passed to the javascript function, and then written out to html in the browser, this same file name looks like this:
\qaserverintranetlibraryformsagent.pdf

How do I avoid this? any help is appreciated!
Liz
 
rubychyld,

If you look at the variable being printed out you are getting:

\qaserverintranetlibraryformsagent.pdf


You are getting the '\' because you escaped the character. Pass the variable as the following:

\\\\qaserver\\intranet\\library\\forms\\agent.pdf

This should give you what you are looking for.

Brandon
 
Thank you Brandon, I now see that I need to add \\ where there is just a \, but since this is a file string built from an asp object, and not just a string that I hard coded, I need some kind of replace method right? something like if inString &quot;\&quot; replace with &quot;\\&quot; but that's as far as my thinking goes, any ideas on where I should go from here?

thanks, Liz
 
hie ribychyld, i think it is somewhere around but i cant get it 2 work (i'm a bit new 2 regular expressions :-()
here is what i got for now
<script>
function regg(str){
var newstr=str.replace(/\x5c/g,%5c%5c)
alert(newstr)
}
regg(&quot;f\\jksdij\k&quot;)
</script>


bye, have a good weekend! regards, vic
 
Regualar Expressions... hmmm, this is not my best subject...

I'm not doing it exactly right, can you help. Where are the rest of my &quot;\&quot; characters in this file path?

function showFileProperties(editablePath)
{
var newEditablePath = editablePath.replace(/\\/g,&quot;\\HELLO&quot;);
alert(newEditablePath)
}

editablePath was: \\qaserver\intranet\library\form.doc
alert gives me:\HELLOqaserverintranetlibraryform.doc

I'm going around in circles... help me catch my tail!!
 
What you need to do is use ASP first to replace all your &quot;\&quot; to &quot;\\&quot; before you pass it to JavasScript:

*****************************
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
function showFileProperties(editablePath){
alert(editablePath);
}
//-->
</SCRIPT>

<%
Response.Write (&quot;<img src=&quot;&quot;library/icons/dir_belllogo.gif&quot;&quot; alt=&quot;&quot;edit&quot;&quot; onclick=&quot;&quot;javascript:showFileProperties(' &quot; & Replace(objFile.Name,&quot;\&quot;,&quot;\\&quot;) & &quot; ')&quot;&quot;>&quot;)
%>

*****************************

- tleish
 
Replace function can only convert the occurence of '\' once. If your string contains more than one '\'. It doesn't help. I used the code below on my site to replace all occurence of a particular character by something else.

function operation(text,add,out) {
while (text.indexOf(out)>-1) {
pos= text.indexOf(out);
text = &quot;&quot; + (text.substring(0, pos) + add +
text.substring((pos + out.length), text.length));
}
return text;
}

function keepsearching(texthere) {
out = &quot;\\&quot;; // replace this
add = &quot;\&quot;; // with this
text = &quot;&quot; + texthere; // temporary holder
text=operation(text,add,out);
return text;
}

Regards
 
thanks for the help, using vbscript replace did replace all occurance of the \, not just the first one, so the string was successfully converted and then passed to my js function just fine...

great advice!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top