ElenaDBA ElenaDBA - 9 months ago 38
HTML Question

Escaping < and > in JavaScript code when assigning it to InnerHtml

I am adding some javascript code to a script tag programmatically by assigning it to InnerHtml property of the HtmlNode

scriptNode.InnerHtml =new_script;

and my javascript contains less than
and/or greater than
characters and that causes problems - distorts my resulting javascript. How can I escape those.

Here is a sample javascript that is causing the problem:

function myFunc(el){
var i=0
for (;i < el.choice.length;i++){

How can I escape the
in the code above while assigning the code as InnerHtml for my Html node?


You can either use HTML codes of > and < which are &lt; and &gt;

OR you can wrap your script within CDATA like this:

        --YOUR SCRIPT--