question

AliAziz-8022 avatar image
0 Votes"
AliAziz-8022 asked Bruce-SqlWork answered

Knockout "html" not rendering target="_blank"

I have a questing about knockout not rendering html attributes properly.
I have a span tag that gets populated with data using knockout JS.

<span id="s34" data-bind="html: $parent.Branch.TextComments"></span>

TextComments is dynamic and sometimes contain text only. Other times text and < a> tags. For example:

  This is text example of TextComment. Access google at <a href="https://www.google.com/" target="_blank">Goole link</a>. Thank you!

When the page reders, the link reders correctly as a url but when I click it, it doesn't open a new tab, it redirects the page to google instead of opening a new tab which target="_blank" should accomplish.

Any ideas?

Thanks in advance!



dotnet-aspnet-general
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I'm not a Knockout developer and this is not a Knockout support forum. But, as far as I know, target="_blank" works as expected. I tested Chrome Edge, and Firefox. Maybe there is a JavaScript event handler catching the anchor click?

Create a standalone HTML page without JavaScript/Knockout and test the link. If it works then the problem is mostly likely in your your Knockout code base. Use the browser's debugger to step through the code if You are not sure where the code is located.

0 Votes 0 ·

1 Answer

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

you binding data must be wrong. this simple test works:

 <div class="liveExample">
     <span data-bind="html: textComments"></span>
 </div>
            
 <script>
     var ViewModel = function(first, last) {
         this.textComments  = ko.observable('<a href="http:google.com" target="_blank">Click Me</a>' );
     };
     ko.applyBindings(new ViewModel()); 
 </script>
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.