JavaScript: encodeURI

By Xah Lee. Date: . Last updated: .

encodeURI is a global function object.

〔►see JavaScript: the Global Object


Return a new string such that illegal URL characters in str are percent encoded.

For example, character such as space, or CIRCUMFLEX ACCENT ^, and Unicode character such as em-dash.

// encode a url
    encodeURI(" cat.jpg")
// encode a url that contains Chinese
// prints

Each encoded character is changed to the form %ab, repeated 1 to 4 times, where a or b is a hexadecimal digit. The 2 digits hexadecimal represents a byte (8 bits). The sequence of bytes together is the character in UTF-8 encoding.


This encoding is called percent encoding. It is required for all Unicode chars in the URI. (but usually browsers can handle it fine without percent encoding.)

What characters are changed by encodeURI?

Printable ASCII chars that are changed are: {{ } [ ] < > % | \ ^ " `}

And all non-ASCII Unicode are also changed.

Characters are changed to a sequence of %dd where dd is 2 hexadecimal digits, and this sequence is the byte sequence of the char by UTF-8 encoding.

For example, the “U+2013: EN DASHem dash” is changed to %E2%80%93.

The following chars are NOT changed by encodeURI:

// unchanged chars of encodeURI

var xx = "-_.!~*'()";
console.log(encodeURI(xx) === xx); // true

var xx = ";,/?:@&=+$#";
console.log(encodeURI(xx) === xx); // true

var xx = "0123456789";
console.log(encodeURI(xx) === xx); // true

console.log(encodeURI(xx) === xx); // true

var xx = "abcdefghijklmnopqrstuvwxyz";
console.log(encodeURI(xx) === xx); // true


ECMAScript® 2016 Language Specification#sec-encodeuri-uri

URL Topic

  1. JavaScript: encodeURI
  2. JavaScript: encodeURIComponent
  3. JavaScript: escape
  4. JavaScript: Get URL (window.location)

String Topic

  1. JavaScript: Default Charset/Encoding
  2. JavaScript: String is 16-Bit Unit Sequence
  3. JavaScript: Unicode Character Escape Sequence
  4. JavaScript: Allowed Characters in Identifier
  5. HTML: Allowed Characters in id Attribute
  6. HTML: Character Sets and Encoding
  7. HTML XML Entities

  1. JavaScript: Template String
  2. JavaScript: Convert String to Number
  3. JavaScript Encode URL, Escape String
  4. JavaScript: Format Number
  5. JavaScript: JSON Object

  1. JavaScript: String Object
  2. JavaScript: String.prototype
  3. JavaScript: String Constructor
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.