user1952811
user1952811

Reputation: 2468

Creating a anchor tag (hyperlink) in a javascript function (Meteor)

I'm trying to create a hyperlink using javascript. Now please remember that this is being done in a function that is not necessarily related to the DOM. So the solutions with manipulating the DOM are not possible.

This is what I've come up with and when I output it, it just comes out as a string, the anchor tag is not interpreted.

        alert_message = 'Wrote a message on <a href="' + url_builder + '"> your profile</a>.';

        return alert_message;

and then alert_message is printed out onto the website. How can I force it to read the html tag? It just treats it as a string and I get the following output.

Output:

"Wrote a message on <a href="/members/tester/#kT6YkY8eDZdGDYQzQ"> your profile</a>." 

Upvotes: 0

Views: 715

Answers (1)

user3374348
user3374348

Reputation: 4101

Strings inserted using {{helper}} in Meteor templates are escaped by default, so embedded HTML will appear in the browser as plain text. You can use the {{{helper}}} (triple braces) to prevent this auto-escaping. In this case, you should be sure you're not opening yourself up to XSS.

Alternatively, you can make another template, something like:

<template name="wroteOnProfileAlert">
  <a href="/members/{{writer.username}}">{{writer.username}}</a>
  wrote a message on
  <a href="/members/tester/#{{user._id}}">your profile</a>
  <!-- if you're using Iron Router, use {{pathFor}} to generate URLs -->
</template>

You should prefer writing another template over generating HTML with string concatenation unless you have some specific reason.

Upvotes: 3

Related Questions