URL encoding
Encode text for use in a URL, or decode it back.
urlencode makes text safe to drop into a URL. urldecode reverses it.
For template authors
Syntax
{urlencode:text}
{urlencode(+):text}
{urldecode:text}Examples
{urlencode:Hello World}
# Hello%20World
{urldecode:Hello%20World}
# Hello WorldPass + as the parameter to use + for spaces, which some search URLs expect:
{urlencode(+):Hello World}
# Hello+WorldBuilding a search link:
https://www.google.com/search?q={urlencode(+):{args}}Aliases
urlencode, encodeuri. Decoding is urldecode only.
For developers
Register the parsers
import { Interpreter, UrlEncodeParser, UrlDecodeParser } from 'tagscript';
const ts = new Interpreter(new UrlEncodeParser(), new UrlDecodeParser());Behaviour
Both require a payload. They call encodeURI and decodeURI, not the Component variants, so reserved URL characters such as &, =, ? and / pass through unchanged. That is fine for a whole URL and wrong for a single query parameter value, where an & in user input would start a new parameter. Write your own parser around encodeURIComponent if you need that.
decodeURI throws on a malformed sequence such as %zz. The interpreter catches it and the render ends with the error as the body.
API reference
Last updated on