3

I have to use some inline code in my asp.net application. F.e. i have following inline code:

<% FlashRenderer.Render(); %>

That will return me following markup:

<div id="flashGame" style="color:red"></div>
  <script type="text/javascript" language="javascript">
     var flash = document.getElementById('flashGame');
     ...
  </script>
</div>

I need to assign a result of FlashRenderer.Render() to javascript variable, then using jquery append that markup to some parent div. I try follwing:

   <script type="text/javascript">
       var swfString = '<%= FlashRenderer.Render() %>';
       $("swf").append(swfString);
   </script>

And it fails, because result string from inline inclusion has both single and double quotes, so when i use single quotes to wrap '<%= FlashRenderer.Render() %>' i always have a syntax error in browser console. I try to replace "'" with "\'" but error happens before i got string created.

Here is screenshot what i have after try to use JavaScriptSerializer().Serialize

Syntax error

3 Answers 3

4

You can use a JSON serializer, which will give you a Javascript-safe string, including the quotes, and it will handle the line breaks as well:

--JSON.NET--:
var x = <%= JsonConvert.SerializeObject(FlashRenderer.Render()) %>;

or

--System.Web.Script.Serialization--:
var x = <%= new JavaScriptSerializer().Serialize(FlashRenderer.Render()) %>;

This will render something like:

var x = "<div id=\"flashGame\" style=\"color:red\"></div>\r\n   <script type=\"text/javascript\" ...etc... </div>";
Sign up to request clarification or add additional context in comments.

8 Comments

I've try both solutions, it results in string like this: "\u003cdiv id=\"flashGame\" style=\"color:red\"\u003e\u003c/div\u003e\u003cscript ... etc and it appended to parent as is, that is also incorrect and i also have syntax error. Possible i need to desserialize before use jquery.append() ?
The \u003c is just unicode-encoded <, so that character is just fine. Assuming the .NET string is written properly, the string value itself is correct (<div id="flashGame" style="color:red;"> etc), so when you write that into the page, it will write correctly. You don't need to do any kind of deserialization. Make sure you are writing exactly as I have it - don't put any quotation marks in yourself.
Thanks for responses. I did it exactly as you said, i use FireFox and FireBug, and here is the screenshot what i have in firebug (attached to main post)
What does FlashRenderer.Render() return? I was assuming it was a string - a string serialized into JSON always includes the quotes. From your image, I'm wondering if it does not actually return the string, but rather writes directly to the response.
The C# string is formatted properly if think because when i use just inline <% %> code statically it renders without any errors
|
1

Instead of trying to place it in a string, just have it render hidden on the page and move it:

<div style="display:none">
  <%= FlashRenderer.Render() %>
</div>

<script type="text/javascript">
  $("swf").append($("#flashGame"));
</script>

4 Comments

I can't because flash object will rendered immediately then, but i need to render it conditionally from JS. I think i can try to escape characters i need on C# side, before creating js string
the javascript will execute inside of the hidden div
Yeah, you could just try a straight up server.urlEncode, then decodeURIComponent() into your jquery string. That might have the same issue though actually...
If you set visibility:hidden instead of display none, does it still load?
1

Hacky solution if you can't modify the C# code. Output the function into a textarea then get the value of the textarea with jQuery.

<textarea id="swfref" style="display:none;">
<% FlashRenderer.Render(); %>
</textarea>

<script type="text/javascript">
    var swfString = $("#swfref").val();
    $("swf").append(swfString);
</script>

2 Comments

One problem here is if you have a </textarea> anywhere in that string, it will terminate the real textarea and bust up the HTML.
@JoeEnos - Yes that's quite a corner case. It's doubtful (considering the OPs specific needs) that he'd be bitten by it, but in the general sense it's an issue. I guess it could be wrapped with a CDATA, but of course if the closing CDATA were to appear in the output we'd be in the same boat.

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.