esamAhmed esamAhmed - 3 months ago 50
ASP.NET (C#) Question

How to correctly use the JavaScriptStringEncode() method?

I want someone to inform me, how to implement

JavaScriptStringEncode()
function and what is used for ?

Answer

you can do like this

<script>
   // wrap in quotes automatically
   alert(<%=HttpUtility.JavaScriptStringEncode(MyStringHere, true)%>);
   // add quotes manually
   alert("<%=HttpUtility.JavaScriptStringEncode(MyStringHere)%>");
   alert("<%=HttpUtility.JavaScriptStringEncode(MyStringHere, false)%>");
</script>

HttpUtility.JavaScriptStringEncode() Method will convert html content to javascript compatible.

Comments