You’re reading the English version of this content since no translation exists yet for this locale. Help us translate this article!
encodeURI() 함수는 URI(Uniform Resource Identifier, 인터넷 식별자)를 각 인스턴스의 특정한 문자들을 한개, 두개, 세개나 네개의 UTF-8인코딩으로 나타내어지는 연속된 확장문자들과 바꾸는 방법으로 부호화 합니다 .(두개의 "대리"문자로 구성된 문자들은 네개의 확장 문자열이 될것입니다. ).
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
구문
encodeURI(URI)
파라미터
URI- 완전한 인터넷 식별자(Uniform Resource Identifier).
리턴 값
주어진 문자열을 URI형태로 암호화하여 새 문자열로 반환합니다.
설명
URI에 대해 특별한 의미 (예약 문자)를 갖는 문자들은 인코딩하지 않습니다. 다음 예는 URI "scheme"이 포함 할 수있는 모든 부분을 보여줍니다. 특정한 문자들이 어떻게 특수한 의미를 가지고 사용되는지에 주의하십시오.
http://username:password@www.example.com:80/path/to/file.php?foo=316&bar=this+has+spaces#anchor
따라서 encodeURI는 완전한 URI를 형성하는데 필요한 문자들을 인코딩하지 않습니다. 또한, encodeURI는 예약된 목적을 가지지는 않지만 현행 URI내에서 허용되는 "예약되지 않은 마크"라고 하는 몇 가지 추가 문자를 인코딩하지 않습니다. (RFC2396 참고)
encodeURI 다음 예외를 제외한 모든 문자를 부호(escape)처리합니다. 예외:
부호처리되지 않는 문자: A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #
encodeURI은 encodeURIComponent과 다음과 같이 다릅니다:
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로 인코딩)
High-low 쌍의 일부가 아닌 대리자를 인코딩하려고 하면 URIError가 예외가 발생됨에 주의하십시오. 예를 들어,
// 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'));
"&", "+"및 "=" 문자는 GET 및 POST 요청에서 특수 문자로 처리되어 인코딩되지 않았기 때문에, encodeURI만으로는 XMLHTTPRequests와 같은 적절한 HTTP GET 및 POST 요청을 구성 할 수 없음에 유의하십시오. 그러나 encodeURIComponent는 이러한 문자를 인코딩합니다.
또한 최신 RFC3986 를 따르고자 한다면 대괄호('[')가 예약어(IPv6를 위해)가 되었으며, URL(호스트 같은)의 일부가 될수 있는 무언가를 형성했을 때 인코딩되지 않는다는 것에 유의하십시오. 다음 코드조각이 도움이 될수 있습니다:
function fixedEncodeURI(str) {
return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']');
}
사양
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 3rd Edition (ECMA-262) | Standard | 초기 정의. |
| ECMAScript 5.1 (ECMA-262) The definition of 'encodeURI' in that specification. |
Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'encodeURI' in that specification. |
Standard | |
| ECMAScript Latest Draft (ECMA-262) The definition of 'encodeURI' in that specification. |
Draft |
브라우저 호환성
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
encodeURI | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 5.5 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Legend
- Full support
- Full support