In my case, I am trying to split the JavaScript Object dynamically into the HTML markup from C#.NET code behind. After getting the data, I prepare the string and create the object in the string and then spit it to the HTML markup.
var fileUploadDic = { 'firstname': 'Jo''hn', 'lastname' : 'Macy' , 'country' : 'USA };
Later on in some other action, such as button click, I tried to pull the firstname, and it gives me a JavaScript error because the value in the firstname property is not escaped to handle the single quote. Although I can do it while preparing the object string in the code backend, I like to do something at the client side instead.
var dv = $('#dv1')
dv.append(fileUploadDic.firstname); //gives me error.
dv.append(fileUploadDic.lastname);
dv.append(fileUploadDic.country);
Is there any way in the JavaScript, that I can do the character escaping while fetching it from the object.
/
In my case, I am trying to split the JavaScript Object dynamically into the HTML markup from C#.NET code behind. After getting the data, I prepare the string and create the object in the string and then spit it to the HTML markup.
var fileUploadDic = { 'firstname': 'Jo''hn', 'lastname' : 'Macy' , 'country' : 'USA };
Later on in some other action, such as button click, I tried to pull the firstname, and it gives me a JavaScript error because the value in the firstname property is not escaped to handle the single quote. Although I can do it while preparing the object string in the code backend, I like to do something at the client side instead.
var dv = $('#dv1')
dv.append(fileUploadDic.firstname); //gives me error.
dv.append(fileUploadDic.lastname);
dv.append(fileUploadDic.country);
Is there any way in the JavaScript, that I can do the character escaping while fetching it from the object.
http://jsfiddle/uagFu/8/
Share Improve this question edited May 18, 2012 at 6:49 jamesmortensen 34k11 gold badges102 silver badges123 bronze badges asked May 18, 2012 at 6:24 KaranKaran 3,32810 gold badges60 silver badges88 bronze badges 1- how about var fileUploadDic = { 'firstname': "Jo'hn", 'lastname' : 'Macy' , 'country' : 'USA' }; ? – jimpic Commented May 18, 2012 at 6:33
4 Answers
Reset to default 7Although you're looking for a client-side solution, the most stable and reliable method of doing this, one that adheres to the Robustness Principle of being conservative in what you send, would be to use a server-side JavaScriptSerializer to serialize your C# object into a JSON string.
These libraries are designed to solve these very problems and eliminate the need for people consuming your API (in this case, just you) from needing to handle the data in a special way, simply because it contains an unterminated string literal.
Employee oEmployee1 =
new Employee{Name="Pini",ID="111", Age="30"};
System.Web.Script.Serialization.JavaScriptSerializer oSerializer =
new System.Web.Script.Serialization.JavaScriptSerializer();
string sJSON = oSerializer.Serialize(oEmployee1);
Next, return sJSON back to the client side from your AJAX request, and process it as you would any other JSON string.
See Convert Objects to JSON in C# Using JavaScriptSerializer for more information and more details.
The first line of code you show is not valid JavaScript:
var fileUploadDic = { 'firstname': 'Jo''hn', 'lastname' : 'Macy' , 'country' : 'USA };
Because the string 'Jo''hn'
can't have single quotes in it like that. It needs to be either 'Jo\'hn'
or "Jo'hn"
. That is to say that if your JS string is quoted with single quotes you have to escape each single quote character within the string with a backslash, but if your JS string is quoted with double quotes you can use singles freely within the string. If you want two single quotes in a row 'Jo\'\'hn'
or "Jo''hn"
.
There is nothing you can do to fix this client-side, because being invalid JavaScript it won't run.
You have to fix it server-side; the easiest way is probably to escape it with a backslash, noting that if that is in a C# string you'll need to escape the backslash too so that the actual output to the browser contains a backslash:
"var fileUploadDic = { 'firstname': 'Jo\\'hn', 'lastname' : 'Macy' , 'country' : 'USA };"
In the string you prepare use:
"var fileUploadDic = { 'firstname': 'Jo\\'hn', 'lastname' : 'Macy' , 'country' : 'USA };"
Now client side that will look like:
var fileUploadDic = { 'firstname': 'Jo\'hn', 'lastname' : 'Macy' , 'country' : 'USA };
If you want to replace ''
client side you can use [varstr].replace(/''/g,"\\'")
, but I think client side will be too late (the Error is already thrown on arrival and interpretation of the sent string)
You could use \'...
jsfiddle
Is that what you want? Actually seems to work with no escaping at all just using a single quote, because you used " double quotes in your definition in jsfiddle...but now I think I don't understand what you want.
If you actually want two single quotes to appear then you can use "Jo\'\'hn"...worked for me.