Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to handle "\x"

    Hi guys. In my system, I'm using javascript to grab current user who logged in to the system. So it will show the current user ID for example like "domain\john". My problem is if the user ID started with character "x" or "u", for example "domain\xia", system will show nothing meaning there is an error occured. Can anybody please help me on this? Thanks in advance.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You need to escape the backslash which otherwise interprets \x and \u as a hex or unicode number.

    var str = "domain\\xia"

    var str = "domain\john"
    alert (str); // domainjohn

    var str = "domain\\john"
    alert (str); // domain\john

    var str = "domain\\xia"
    alert (str); // domain\xia

    But it is not a good idea to use a backslash as a delimiter. Javascript will interpret the backslash character as an 'escape' character to signal some special interpretation of the following character.


    Frustration - the first time you can't do it the second time.
    Desperation - the second time you can't do it the first time
    Last edited by Philip M; 09-06-2012 at 10:56 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I only can put that double backslash if the user ID value is define in the javascript function itself. The problem is the value of the user ID is automatically get from funtion "<%= SPContext.Current.Web.CurrentUser.LoginName %>", so I can't put double slash in the user ID value. Any idea please...

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    As I say, Javascript interprets a backslash as a control character. Replace() does not work as \x is not two separate characters, but one control character. You must either ban user ids which start with u or x, or revise your server-side code to change the delimiter to something other than a backslash. Note that domain\john is rendered as domainjohn in Javascript as the backslash is ignored here.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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