How to stop interpolation of Escape character in variable?

Discussion in 'ASP .Net' started by \Rob\, Feb 22, 2006.

  1. \Rob\

    \Rob\ Guest

    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
    \Rob\, Feb 22, 2006
    #1
    1. Advertising

  2. \Rob\

    Darren Kopp Guest

    Darren Kopp, Feb 22, 2006
    #2
    1. Advertising

  3. "Rob" wrote:
    > 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
    Daniel Kirsch, Feb 22, 2006
    #3
  4. \Rob\

    Darren Kopp Guest

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

    -Darren
    Darren Kopp, Feb 22, 2006
    #4
  5. \Rob\

    intrader Guest

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


    On Wed, 22 Feb 2006 10:09:22 -0800, Darren Kopp wrote:

    > Request.ServerVariables("LOGON_USER").Replace('\r', '') should work.
    >
    > HTH,
    > Darren Kopp
    > http://blog.secudocs.com/
    intrader, Feb 22, 2006
    #5
  6. \Rob\

    \Rob\ Guest

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

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

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

    "Darren Kopp" <> wrote in message
    news:...
    > Hahaha, he's right, i thought you meant \r at the end. Yea the \r in
    > \rob is needs to be \\rob.
    >
    > -Darren
    >
    \Rob\, Feb 23, 2006
    #6
  7. \Rob\

    Darren Kopp Guest

    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/
    Darren Kopp, Feb 23, 2006
    #7
  8. "Rob" wrote:
    > 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"),"\","\\") ) %>'


    --
    Microsoft MVP -- ASP/ASP.NET
    Please reply to the newsgroup. The email account listed in my From
    header is my spam trap, so I don't check it very often. You will get a
    quicker response by posting to the newsgroup.
    Bob Barrows [MVP], Feb 23, 2006
    #8
  9. Darren Kopp wrote:
    > 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
    Daniel Kirsch, Feb 24, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Bunker

    single/double quote escape interpolation

    Simon Bunker, Jul 7, 2003, in forum: Python
    Replies:
    2
    Views:
    646
    Bengt Richter
    Jul 8, 2003
  2. slomo
    Replies:
    5
    Views:
    1,493
    Duncan Booth
    Dec 2, 2007
  3. Scott Bass
    Replies:
    11
    Views:
    180
    Brian McCauley
    May 29, 2005
  4. phrankster

    Variable Interpolation with %%variable

    phrankster, Aug 9, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    129
    James Taylor
    Aug 14, 2005
  5. Bart Van der Donck

    Assignment of var without escape/interpolation

    Bart Van der Donck, Apr 15, 2006, in forum: Perl Misc
    Replies:
    16
    Views:
    202
    Peter J. Holzer
    Apr 17, 2006
Loading...

Share This Page