How to stop interpolation of Escape character in variable?

R

\Rob\

I have this code.....

<script language="JavaScript">
function getLogonUser() {

var u = '<% Response.Write(
Request.ServerVariables("LOGON_USER") ) %>'
alert( u )
}
</script>

......should return "MYDOMAIN\rob.test".....but the "\r" turns into a
Carriage return

Anybody know how to prevent this from happening?

Thanks
 
D

Daniel Kirsch

Rob said:
I have this code.....

<script language="JavaScript">
function getLogonUser() {

var u = '<% Response.Write(
Request.ServerVariables("LOGON_USER") ) %>'
alert( u )
}
</script>

.....should return "MYDOMAIN\rob.test".....but the "\r" turns into a
Carriage return

Anybody know how to prevent this from happening?

You have to use a double backslash:

MYDOMAIN\\rob.test

Otherwise the character following the "\" will be interpreted as kind of
a special instruction character. That's the reason why path names have
double backslashes in many programming languages.

Daniel
 
D

Darren Kopp

Hahaha, he's right, i thought you meant \r at the end. Yea the \r in
\rob is needs to be \\rob.

-Darren
 
I

intrader

It would also work if Microsoft follows the lead (over 40 years) of Unix
and use '/' instead.
 
R

\Rob\

Thanks, but I don't have the option to do that.....

<% Response.Write( Request.ServerVariables("LOGON_USER") ) %>

......is returning it to me.
 
D

Darren Kopp

Is this classic ASP or ASP.NET?

If it's asp.net, the do
Response.Write(Request.ServerVariables("LOGON_USER").Replace('\',
'\\'))

You could also just insert another \ at the IndexOf('\')

HTH,
Darren Kopp
http://blog.secudocs.com/
 
B

Bob Barrows [MVP]

Rob said:
I have this code.....

<script language="JavaScript">
function getLogonUser() {

var u = '<% Response.Write(
Request.ServerVariables("LOGON_USER") ) %>'
alert( u )
}
</script>

.....should return "MYDOMAIN\rob.test".....but the "\r" turns into a
Carriage return

Anybody know how to prevent this from happening?
Double the backslash:

var u = '<% Response.Write(
Replace(Request.ServerVariables("LOGON_USER"),"\","\\") ) %>'
 
D

Daniel Kirsch

Darren said:
If it's asp.net, the do
Response.Write(Request.ServerVariables("LOGON_USER").Replace('\',
'\\'))

Not sure, if asp.net will handle single backslashes, but in JavaScript
you need to escape them:

Replace('\\', '\\\\'))
You could also just insert another \ at the IndexOf('\')

That should than be done for every "\" not only for the first one which
IndexOf('\') will probably return.

Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top