Vaultools
Menu

Percent-encoding

Percent-encoding, also called URL encoding, writes a character as a percent sign followed by the hexadecimal value of each of its UTF-8 bytes, such as %20 for a space, so text can be carried safely inside a URL. It is defined in RFC 3986.

How it works

A URL can contain only a limited set of characters, and some of those characters have jobs: / separates path segments, ? starts the query, & separates parameters, = joins a key to its value and # starts the fragment. To put any other character in a URL, or to use one of those characters as plain data, you replace each byte with % and two hex digits.

Letters, digits and - . _ ~ are the unreserved characters and never need encoding.

Which characters to encode depends on where they go

The same text needs different treatment in different places, which is why JavaScript has two built-in functions:

FunctionUse forEncodes / ? & = #
encodeURIComponentA single value inside a URLYes
encodeURIA whole URL that has spaces or accents in itNo

HTML forms use a variant, application/x-www-form-urlencoded, where a space becomes + instead of %20.

Common pitfalls

References

Ads on this page

Non-personalized ads help keep Vaultools free — Google decides where they appear on the page.

Go Pro to remove them →