2

I have this code:

Dim script = New HtmlGenericControl("script")
script.Attributes.Add("type", "text/javascript")
script.InnerText = "var alohaInterval = setInterval(function() {if (typeof Aloha === ""undefined"") {return;} clearInterval(alohaInterval); Aloha.ready(function() {Aloha.jQuery("".editable-content"").aloha();})}, 100);"
htmlControl.Controls.Add(script)

This generates the following thing:

<script type="text/javascript">var alohaInterval = setInterval(function() {if (typeof Aloha === &quot;undefined&quot;) {return;} clearInterval(alohaInterval); Aloha.ready(function() {Aloha.jQuery(&quot;.editable-content&quot;).aloha();})}, 100);</script>

The problem is that it is HTML-encoded. My question is: how to make sure that the script will not be HTML-encoded?

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • 1
    Use RegisterClientScriptBlock. https://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerclientscriptblock(v=vs.110).aspx – Bert Aug 10 '16 at 19:05
  • I will soon test that. Can you explain why is the result of the code shown in the question HTML-encoded? – Lajos Arpad Aug 10 '16 at 19:07
  • InnerText encodes the text. InnerHtml should allow it to pass through un-encoded. – Bert Aug 10 '16 at 19:09
  • @BertEvans, I have tested with InnerHtml as well before I have written the question and I experienced the same behavior. – Lajos Arpad Aug 10 '16 at 19:10
  • I'm not sure then. Looking at the source, it clearly is showing InnerText encoded, but I don't see InnerHtml being encoded. Maybe VB? http://referencesource.microsoft.com/#System.Web/UI/HtmlControls/HtmlContainerControl.cs,31ccfd9c0c0fbcbe,references – Bert Aug 10 '16 at 19:15
  • I was executing it with Visual Basic, indeed. – Lajos Arpad Aug 10 '16 at 19:17
  • 1
    https://support.microsoft.com/en-us/kb/815186 maybe? – Bert Aug 10 '16 at 19:20
  • That seems to be the exact issue. – Lajos Arpad Aug 10 '16 at 19:20
  • The solution is ClientScript.RegisterClientScriptBlock(Me.GetType, "alohainit", "var alohaInterval = setInterval(function() {if (typeof Aloha === ""undefined"") {return;} clearInterval(alohaInterval); Aloha.ready(function() {Aloha.jQuery("".editable-content"").aloha();})}, 100);", True) indeed, exactly as you suggested. If you want, you can write an answer and I will accept it. If not, then tomorrow I will write the answer myself. Thanks for the pointers! – Lajos Arpad Aug 10 '16 at 19:24

1 Answers1

1

There are several ways you may want to go about this depending on what you need.

For a small amount of script that you always want available for your control, that you are most commonly embedding in the code as a string, you will want to use RegisterClientScriptBlock or RegisterStartupScript. Which one you choose depends mainly on where you want the script to be rendered. You can find a good explanation for when to choose one or the other here.

Another option you may want to choose is RegisterClientScriptResource. I find this option most useful when I am writing a server control that has a significant amount of JavaScript or that I want to include as a part of a class library. Using this option you can compile the script into the library as an embedded resource and then include the library in other projects. The editing experience is just that much better when I have more than just a couple lines of JavaScript.

For this particular case:

ClientScript.RegisterClientScriptBlock(Me.GetType, "alohainit", "var alohaInterval = setInterval(function() {if (typeof Aloha === ""undefined"") {return;} clearInterval(alohaInterval); Aloha.ready(function() {Aloha.jQuery("".editable-content"").aloha();})}, 100);", True)
Bert
  • 80,741
  • 17
  • 199
  • 164