Tools / URL encoder
URL encoder, decoder and parser
Two encoders, one decoder and a parser. The difference between the two encoders is the most common source of double-encoded or broken links.
Encode and decode
Examples:
Split a URL
| Name | Value |
|---|
Which encoder
| Function | Leaves alone | Use for |
|---|---|---|
encodeURIComponent | A-Z a-z 0-9 - _ . ! ~ * ' ( ) | one query value, one path segment, anything you insert into a URL |
encodeURI | the above plus ; , / ? : @ & = + $ # | a complete URL that is already structurally correct |
Run encodeURI on a query value and & survives, so a&b becomes two parameters. Run encodeURIComponent on a whole URL and every / becomes %2F. When building a query string, URLSearchParams handles both cases and is usually the better choice.
Pitfalls
HTML forms encode spaces as +, but decodeURIComponent leaves + alone. Tick the option when decoding form bodies or query strings produced by forms.
A lone % or %zz makes decodeURIComponent throw URIError. This page reports the position instead. Decoding twice is a bug of its own: %2541 decodes to %41, and a second pass turns that into A.
The parser uses the WHATWG URL class, the same one browsers use for links. It lowercases the host, converts internationalised domain names to punycode and drops a default port such as :443.