HTML Encoding Not Working RRS feed

  • Question

  • User898185877 posted


    I've written a web service (ASMX) that returns HTML.  When one of my methods returns HTML, it does so in an encoded format:

    return Server.HtmlEncode(l_HtmlResult);

    In my client-side JavaScript on the page that retrieves this result, I get the encoded HTML with no problems.  However, when I try to update a DIVs innerHTML property with the HTML returned from my web service, it displays the actual HTML code instead of rendered HTML.

    var windowDiv = document.getElementById('account_locked_div');
    windowDiv.innerHTML = resultingHtml;

    So insead of seeing :

    Account Locked

    Your account has been locked...

    I get this instead:


    <h3>Account Locked<h3>
    <p>Your account has been locked...</p>


    If I don't call HtmlEncode when returning it renders properly, although that is somewhat unsafe.  Any reasons why it won't render HTML encoded code?  Thanks.

    Wednesday, April 16, 2008 12:02 PM


  • User-821857111 posted

    Seems to me that HTMLEncode is doing exactly what it is supposed to.  If you want to preserve the html so that is acts as html on the page, don't htmlencode it.  HTMLEncode will change < to &lt;, and > to &gt;.  That's its job.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 16, 2008 12:31 PM