Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-06-2012, 10:01 AM   PM User | #1
S_henry
New to the CF scene

 
Join Date: Sep 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
S_henry is an unknown quantity at this point
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.
S_henry is offline   Reply With Quote
Old 09-06-2012, 10:34 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,042
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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
__________________

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.

Last edited by Philip M; 09-06-2012 at 10:56 AM..
Philip M is offline   Reply With Quote
Old 09-06-2012, 10:53 AM   PM User | #3
S_henry
New to the CF scene

 
Join Date: Sep 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
S_henry is an unknown quantity at this point
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...
S_henry is offline   Reply With Quote
Old 09-06-2012, 12:02 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,042
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:22 PM.


Advertisement
Log in to turn off these ads.