Skip to content
BENIBIX Learn

URL Encoding (Percent-Encoding)

Development📄 Files & FormatsText

Also known as Percent-Encoding, Percent Encoding, URL Escape, encodeURIComponent

Writing unsafe characters in a web address as % codes. A space becomes %20.

Think of it like

Think of it like a secret code. It lets odd signs pass where they are not allowed.

Example

Lucy's code builds a search link for fish & chips. It writes fish%20%26%20chips, so the & is not read as the start of a new field.

Why it matters

In a link, the & and the ? and the / all have jobs. Encoding stops your text being mistaken for them.

Definition

URL encoding writes unsafe characters in a web address as safe codes. Each one becomes a percent sign and two hex digits. A space becomes %20, and & becomes %26. Letters, digits and the four marks - . _ ~ stay as they are. Browsers do this as you type, but code that builds links must do it on purpose.

Example code

javascript
encodeURIComponent("fish & chips")
// 'fish%20%26%20chips'
Each space becomes %20 and the & becomes %26, so the text is safe in a link.
The bottom row: fish & chips becomes fish%20%26%20chips inside a link.

Common mistake

Encoding a value twice. Then %20 becomes %2520, and the page shows a stray %20 instead of a space.

Key takeaways

  1. 1
    Percent plus hex. Each unsafe byte becomes a % code.
  2. 2
    A safe set stays. Letters, digits and four marks do not change.
  3. 3
    Encode each value. Do it for every piece you add.

Learn it properly