JavaScript - exclude single quote in property value of JavaScript object

In my case, I am trying to split a JavaScript object dynamically into HTML markup from C # .NET code behind. After receiving the data, I prepare the line and create an object in the line, and then spit on the HTML markup.

var fileUploadDic = { 'firstname': 'Jo''hn', 'lastname' : 'Macy' , 'country' : 'USA }; 

Later in some other actions, such as clicking a button, I tried to pull out the first name, and this gives me a JavaScript error, because the value in the firstname property is not escaped to handle a single quote. Although I can do this while preparing the object string in the code backend, I like to do something on the client side.

 var dv = $('#dv1') dv.append(fileUploadDic.firstname); //gives me error. dv.append(fileUploadDic.lastname); dv.append(fileUploadDic.country); 

Is there any way in JavaScript that I can escap a character while retrieving it from an object.

http://jsfiddle.net/uagFu/8/

+6
source share
4 answers

The first line of code you show is invalid JavaScript:

 var fileUploadDic = { 'firstname': 'Jo''hn', 'lastname' : 'Macy' , 'country' : 'USA }; 

Because the string 'Jo''hn' cannot have single quotes in it. It must be either 'Jo\'hn' or "Jo'hn" . That is, if your JS string is quoted with single quotes, you need to avoid each quote character inside the backslash string, but if your JS string is quoted with double quotes, you can freely use singles within the string. If you need two single quotes in the string 'Jo\'\'hn' or "Jo''hn" .

You cannot fix anything in order to fix this client side, because being invalid, JavaScript will not work.

You must fix it on the server side; the easiest way is probably to avoid it with a backslash, noting that if it is in a C # line, you will also need to avoid a backslash so that the actual output to the browser contains a backslash:

 "var fileUploadDic = { 'firstname': 'Jo\\'hn', 'lastname' : 'Macy' , 'country' : 'USA };" 
+2
source

Although you are looking for a client-side solution, the most stable and reliable way to do this, the one that adheres to the principle of being conservative in what you send, would use server-side JavaScriptSerializer to serialize your C # object to a JSON string.

These libraries are designed to solve these problems and eliminate the need for people consuming your API (in this case, only you) to need data processing in a special way, simply because it contains an inexhaustible 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); 

Then, return sJSON back to the client side from your AJAX request and process it like any other JSON string.

See Converting objects to JSON in C # using the JavaScriptSerializer for more information and more details.

+7
source

In the line you are preparing, use:

 "var fileUploadDic = { 'firstname': 'Jo\\'hn', 'lastname' : 'Macy' , 'country' : 'USA };" 

Now the client side will look like this:

 var fileUploadDic = { 'firstname': 'Jo\'hn', 'lastname' : 'Macy' , 'country' : 'USA }; 

If you want to replace the client side '' , you can use [varstr].replace(/''/g,"\\'") , but I think that the client side will be too late (an error has already been selected upon arrival and interpretation sent strings)

+2
source

You can use \"...

jsfiddle

Is this what you want? Actually, it doesn't seem to work at all without 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 really need two single quotes, you can use "Jo \ '\' hn" ... for me.

0
source

Source: https://habr.com/ru/post/916063/


All Articles