[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/encodeURI [Back]  [Original]

encodeURI() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

encodeURI()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

encodeURI() URI UTF-8 , , , . ( .)

In this article

const uri = "https://mozilla.org/?x=";
const encoded = encodeURI(uri);
console.log(encoded);
// Expected output: "https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B"

try {
  console.log(decodeURI(encoded));
  // Expected output: "https://mozilla.org/?x="
} catch (e) {
  // Catches a malformed URI
  console.error(e);
}

js
encodeURI(URI);

URI

URI.

URI .

encodeURI() URI ( ) . "URI " . .

http://username:password@www.example.com:80/path/to/file.php?foo=316&bar=this+has+spaces#anchor

encodeURI() URI . , URI (" ") . (RFC 2396 )

encodeURI() .

   :

    A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #

encodeURI() encodeURIComponent() .

js
var set1 = ";,/?:@&=+$#"; //  
var set2 = "-_.!~*'()"; //  
var set3 = "ABC abc 123"; //   , 

console.log(encodeURI(set1)); // ;,/?:@&=+$#
console.log(encodeURI(set2)); // -_.!~*'()
console.log(encodeURI(set3)); // ABC%20abc%20123 ( %20 )

console.log(encodeURIComponent(set1)); // %3B%2C%2F%3F%3A%40%26%3D%2B%24%23
console.log(encodeURIComponent(set2)); // -_.!~*'()
console.log(encodeURIComponent(set3)); // ABC%20abc%20123 ( %20 )

encodeURI() XMLHttpRequest , HTTP GET POST . GET POST "&", "+", "=" . encodeURIComponent() .

- URIError .

js
// high-low pair ok
console.log(encodeURIComponent("\uD800\uDFFF"));

// lone high surrogate throws "URIError: malformed URI sequence"
console.log(encodeURIComponent("\uD800"));

// lone low surrogate throws "URIError: malformed URI sequence"
console.log(encodeURIComponent("\uDFFF"));

, URL RFC RFC3986 , IPv6 encodeURI() URL . .

js
function fixedEncodeURI(str) {
  return encodeURI(str).replace(/%5B/g, "[").replace(/%5D/g, "]");
}

Specification
ECMAScript 2027 LanguageSpecification
# sec-encodeuri-uri


Web Proxy Viewer  |  New URL  |  Original Page