1

On the server side, I assign the click event of a button

closeWindow.OnClickFunc = String.Format("CloseWindow('{0}');", "{\"codeA\":\"MALE\",\"codeB\":\"SomeCodeB\"}");

When I examine the rendered HTML, it's completely malformed:

<img onclick="CloseWindow('{" src="close.jpg" id="closeLink">

To debug the issue, I got rid of the double quotes in the json string

closeWindow.OnClickFunc = String.Format("CloseWindow('{0}');", "{codeA:MALE,codeB:SomeCodeB}");

Now the HTML is as expected:

<img onclick="CloseWindow('{sectionCodeDB:NEWS,sectionCodeDBNew:NEWS,itemType:REMINDER}');" src="close.jpg" id="closeLink">

So, there's some issue with the double quotes that's messing the HTML.

Any thoughts on how to pass the JSON string?

3
  • 5
    You actually don't need to pass it as JSON string, pass it as JavaScript object. Have you tried: String.Format("CloseWindow({0});", "{codeA:'MALE',codeB:'SomeCodeB'}") ? (if you use JSON.parse in CloseWindow, you don't need that anymore) Commented Feb 16, 2011 at 20:25
  • 2
    @Felix, you probably should wrap that comment in an answer :) Commented Feb 16, 2011 at 20:32
  • @Frédéric Hamidi: Ok. Done ;) Commented Feb 16, 2011 at 20:37

3 Answers 3

3

Disclaimer: I have never used asp.net, but string formatting functions cannot be so different, right? ;)

Actually, you don't need to pass it as JSON string, pass it as JavaScript object. Try:

closeWindow.OnClickFunc = String.Format("CloseWindow({0});", "{codeA:'MALE',codeB:'SomeCodeB'}")
//                                      no quotes ---^-^

If you used JSON.parse in CloseWindow, you don't need that anymore. You can access the data directly from the parameter, like: param.codeA.

Update:

Regarding escaping issues. If you have

"{\"codeA\":\"MALE\",\"codeB\":\"SomeCodeB\"}"

then this will only escape the quotes in the server side string. The generated code would like :

 <img onclick="CloseWindow('{"codeA":"MALE","codeB":"SomeCodeB"}'" src="close.jpg" id="closeLink">

which is clearly invalid. You would need

<img onclick="CloseWindow('{\"codeA\":\"MALE\",\"codeB\":\"SomeCodeB\"}'" src="close.jpg" id="closeLink">

Now I think you can achieve this with three backslashes in the string:

String.Format("CloseWindow('{0}');", "{\\\"codeA\\\":\\\"MALE\\\",\\\"codeB\\\":\\\"SomeCodeB\\\"}");

There might be a better way though. As I said, I don't know asp.net.

Nevertheless, it is unnecessary to pass the data as JSON string. A plain JavaScript is way simpler.

Sign up to request clarification or add additional context in comments.

2 Comments

This is not necessarily related to JSON. Any string I create on the serverside that has quotes causes this issue.
@DotnetDude: Maybe. But your second example only contains single quotes (and this worked). So the above should work at least for this instance. If you don't create JSON, you don't need double quotes. I think \" only escapes the quote in the server side string. If you also want to escape it in the generated code, you need three backslahes: \\\", which should produce an escaped string: \".
2

You need to encode / escape the quotes in HTML, just like you escape the quotes in C# / VB.NET.

Server.HtmlEncode(String.Format("CloseWindow('{0}');", "{\"codeA\":\"MALE\",\"codeB\":\"SomeCodeB\"}"));

Comments

-1
<img onclick="CloseWindow(eval('[{a:123, b:456}]')[0]);" />

add [ ] to string b4 eval

4 Comments

Besides quoting issues, eval is totally unnecessary here. Are you using txtspk? You know, there is no character limits for answers.
Does this make a dancing unicorn appear on the screen? If not, I wouldn't use this method.
@Chris Shouts: You mean like this? (ok, it's running, not dancing... but there is music!).
for quoting, just miss typeing. and for text, the topic ask for json string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.