0

I'm adding a custom link to a SendGrid template.

Part of the link is some url query parameters. They're separated by an ampersand (&), like so:

<a href="www.example.com?arg1=foo&arg2=bar">My Link</a>

Sendgrid keeps auto-updating the string above, escaping the character and creating this:

<a href="www.example.com?arg1=foo&amp;arg2=bar">My Link</a>

This is not the same link at all, and I would like it to not force character escaping in my custom html code.

And no, these two links are not the same

const t1 = new URLSearchParams("a=1&amp;b=2");
console.log(t1.get("a")); // 1
console.log(t1.get("b")); // null
console.log(t1.get("amp;b")); // 2

const t2 = new URLSearchParams("a=1&b=2");
console.log(t2.get("a")); // 1
console.log(t2.get("b")); // 2

How can I make SendGrid allow me to use a ampersand?

  • Does it end up in the raw generated email as &, &amp;, or &amp;amp;? – grawity May 15 at 19:36
  • &amp;. Exactly as shown above – Seph Reed May 15 at 19:44
  • 1
    Then there is no problem. That is what an HTML file is supposed to have. – grawity May 15 at 19:49
  • I'm working with a url, not an html file. Escaping characters is something you do for text in an html file, and certain characters in url sometimes. This is not one of those cases, I'd like the characters to not be escaped. See my edit for details on why having &amp; does not work. – Seph Reed May 15 at 20:25
  • Might not be an issue but are your actual links like <a href="www…? Where is the HTTP or HTTPS? Should it not be <a href="http://www… or <a href="https://www…? – JakeGould May 15 at 20:38
-1

This is normal and does not change the meaning of the URL, as it will be automatically decoded back to & when the HTML document is parsed.

Generally, in HTML and XHTML, a lone ampersand must be written as a 'character entity reference' in order to avoid possible confusion with actual HTML entity references (such as "&lt;" or "&quot;"). All parsers recognize the named reference &amp; although variants such as &#38; are also legal.

HTML 4:

Authors should use "&amp;" (ASCII decimal 38) instead of "&" to avoid confusion with the beginning of a character reference (entity reference open delimiter). Authors should also use "&amp;" in attribute values since character references are allowed within CDATA attribute values.

HTML 5:

Attribute values are a mixture of text and character references, except with the additional restriction that the text cannot contain an ambiguous ampersand. [...]

8.2.4.72. Character reference state

If no match can be made and the temporary buffer consists of a U+0026 AMPERSAND character (&) followed by a sequence of one or more alphanumeric ASCII characters and a U+003B SEMICOLON character (;), then this is a parse error. [...] If the last character matched is not a U+003B SEMICOLON character (;), this is a parse error.

XHTML 1:

In order to ensure that documents are compatible with historical HTML user agents and XML-based user agents, ampersands used in a document that are to be treated as literal characters must be expressed themselves as an entity reference (e.g. "&amp;"). For example, when the href attribute of the a element refers to a CGI script that takes parameters, it must be expressed as http://my.site.dom/cgi-bin/myscript.pl?class=guest&amp;name=user rather than as http://my.site.dom/cgi-bin/myscript.pl?class=guest&name=user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.