Search code examples
javascriptknockout.jsjquery-templates

How to prevent auto parsing of an URL tag and render it as the full tag instead?


Does anyone know if KnockoutJS parses < and > in its observables? I am returning a string such as...

<a href="#">google.com</a>

...but it renders as...

&lt;a href="www.google.com" target="_blank" style="color: #0065CB"&gt;www.google.com&lt;/a&gt;

...and so the url is not rendered properly. Here is the template, the tag with the problem is ${ text }:

<script type="text/html" id="chatRoom">
<div id="chatContainer" class="chatContainer">
    <div class="chatFrom">
        oaSES Sales
        <i id="chatClose" class="chatSprite chatClose" data-bind='click: function() { server.removeChat(this) }'></i>
    </div>
    <div class="chatMessages">
        <ul id="chatHolder">
        {{each messages()}}
            <li><div class="chatFromText">From: ${ from }</div>
            <div class="chatTime">${ time }</div><div class="chatMsg">${ text }</div></li>
        {{/each}}
        </ul>
    </div>
    <div class="chatControls">
    <form data-bind="submit: function() { send($('#'+channel).val()); $('#'+channel).focus(); }">
        <input type="text" id="${ channel }" name="message" class="chatText" style="color: #999;" value="Message Here" data-bind='click: function() {
            $("#"+channel).val("").css("color", "#000");
        }'  />
        <i class="chatSprite chatSend" data-bind="click: function() { $('.chatSend').parent().submit() }"></i>
    </form>
    </div>
</div>
</script>

Solution

  • You will want to use {{html text}} instead of ${ text } to make sure that your value is not escaped. If you are using data-bind attributes in KO, then there is a "html" binding that can be used as an alternative to the "text" binding.