JS: encodeURI

By Xah Lee. Date: . Last updated: .

encodeURI is a string property of the the Global Object.

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

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.

// encode a url that contains space

console.log(
 encodeURI ( "http://example.com/a b.jpg" ) ===
 "http://example.com/a%20b.jpg"
);

// true
// encode Chinese characters

console.log(
encodeURI("δΈ­ζ–‡") === "%E4%B8%AD%E6%96%87"
);
// true

// δΈ­
// codepoint 20013
// hexadecimal 4e2d
// utf8 encoding: E4 B8 AD

// ζ–‡
// codepoint 25991
// hexadecimal 6587
// utf8 encoding: E6 96 87
// encode a emoji
console.log( encodeURI("πŸ˜‚") === "%F0%9F%98%82" );
// true

// πŸ˜‚
// name: FACE WITH TEARS OF JOY
// codepoint decimal: 128514
// codepoint hexadecimal: 1f602
// utf8 encoding in hexadecimal : F0 9F 98 82

This encoding is called percent encoding. It is required for all non-ASCII 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.

The following chars are NOT changed by encodeURI:

// unchanged chars of encodeURI

let xx;

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

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

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

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

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

back to Encode URL, Escape String

BUY Ξ£JS JavaScript in Depth