How to Keep URLs with Ampersands Valid in HTML Links

Learn when to use & and %26 in an HTML href, with examples of multiple query parameters and JavaScript URL construction.

Published
Reading time
4 min read
How to Keep URLs with Ampersands Valid in HTML Links
On this page

An ampersand in an HTML URL href can have two different jobs: it can separate query parameters in the URL, or it can be part of HTML syntax. To write a valid link by hand, percent-encode special characters that belong inside parameter values, then represent each remaining URL ampersand as & in the HTML attribute.

Key Takeaways

  • In a URL, & separates query parameters; an ampersand inside a parameter value should usually be encoded as %26.
  • In HTML source, write a URL separator as & inside the href attribute.
  • The browser decodes & when it parses the HTML. It does not send the letters amp; as part of that separator.
  • HTML escaping and URL percent-encoding solve different problems. Apply them at the appropriate stage.

Write an ampersand in an HTML URL href

Suppose the URL has two query parameters, q and lang. Write the link in HTML like this:

<a href="https://example.com/search?q=tea&amp;lang=en">Search</a>

The HTML parser turns &amp; into & in the attribute value. When the link is followed, the resulting URL is:

https://example.com/search?q=tea&lang=en

The server receives a query string with q=tea and lang=en. The &amp; spelling belongs to the HTML source, not to the URL you would paste into an address bar.

Some browsers appear to accept a bare & in HTML attributes. Do not rely on that recovery behavior: HTML can interpret ampersands as the start of character references. Writing &amp; makes the intended separator explicit.

Advertisement

Encode special characters inside a query value

Now suppose the search value is fish & chips, and the URL also has lang and sort parameters. The ampersand in the search value is data, not a parameter separator, so encode it as %26. This example also encodes the spaces as %20:

<a href="https://example.com/search?q=fish%20%26%20chips&amp;lang=en&amp;sort=price">Search for fish and chips</a>

After HTML parsing, the URL is:

https://example.com/search?q=fish%20%26%20chips&lang=en&sort=price

A server that parses and decodes the query parameters will typically see:

q    = fish & chips
lang = en
sort = price

Here, %26 stays within the q value, while the two & characters separate parameters. If the value's ampersand were left unencoded, a query parser could treat the text after it as another parameter instead.

The same distinction matters for other reserved characters. For example, an unencoded # starts a URL fragment rather than remaining in the query; a literal # in a query value should be encoded as %23. A literal plus sign in a form-style query value should be encoded as %2B, because form-style query parsers commonly interpret + as a space.

Build a query string with JavaScript

For values supplied at runtime, use URL and URLSearchParams rather than concatenating strings. This browser example creates a link and assigns its URL through the DOM:

<a id="search-link">Search</a>

<script>
  const url = new URL("https://example.com/search");
  url.searchParams.set("q", "fish & chips");
  url.searchParams.set("lang", "en");
  url.searchParams.set("sort", "price");

  document.getElementById("search-link").href = url.toString();
</script>

URLSearchParams encodes the values as it serializes the query. Its form-style serialization represents the space as +, so the resulting URL is:

https://example.com/search?q=fish+%26+chips&lang=en&sort=price

Do not pre-encode the values before passing them to searchParams.set(): doing so can encode the percent signs again. Because this example assigns the URL through the DOM, it does not need to turn separators into &amp; first. If you instead insert a URL into an HTML source string or template, use the template system's HTML attribute escaping after constructing the URL.

Advertisement

Common Mistakes

  • Writing %26 between parameters: This encodes the ampersand as data instead of providing a query separator.
  • Writing &amp; into a URL passed directly to JavaScript or pasted into an address bar: Outside HTML parsing, those characters are literal URL text; they are not automatically converted to &.
  • Escaping the whole URL with a value encoder: Encode individual query values or use URLSearchParams; encoding the whole URL can also encode structural characters such as : and /.
  • Assuming URL encoding makes a string safe for HTML: A URL inserted into HTML still needs context-appropriate attribute escaping. A properly escaped attribute also does not make an untrusted destination safe: validate allowed schemes and destinations separately.

Best Practices

  1. Construct the URL from its components, encoding query values rather than separators.
  2. When writing HTML source, quote the href and use &amp; for each remaining ampersand in the URL. Escape other characters as required by the attribute context.
  3. For dynamic links, prefer DOM APIs and URLSearchParams over manual string concatenation.
  4. Check both the rendered link destination and how your server parses its parameters, especially when values contain &, +, #, or spaces.

This explanation was prepared without access to documentation-retrieval or code-execution tools. Consult the current HTML and URL standards and verify the example in your target environment before publication.

Conclusion

Use %26 for an ampersand that belongs inside a query value and &amp; for a URL separator written in HTML source. The browser resolves the HTML character reference before following the link.

What would your domain sell for?

A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.