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

encodeURIComponent() - 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

encodeURIComponent()

Baseline Widely available

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

encodeURIComponent() - , (URI) , , , UTF-8 ( 4 , 2 "" ).

In this article

encodeURIComponent(str);

str

. URI.

encodeURIComponent , : , , - _ . ! ~ * ' ( )

: URIError , , - , :

js
// -  - 
console.log(encodeURIComponent("\uD800\uDFFF"));

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

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

, encodeURIComponent , URI. , "Thyme &time=again" comment. encodeURIComponent comment=Thyme%20&time=again. , -. "Thyme &time=again", POST , - "Thyme", (time) "again".

application/x-www-form-urlencoded "+", encodeURIComponent "%20" "+".

RFC 3986 ( !, ', (, ), *), , URI , :

js
function fixedEncodeURIComponent(str) {
  return encodeURIComponent(str).replace(/[!'()*]/g, function (c) {
    return "%" + c.charCodeAt(0).toString(16);
  });
}

, UTF-8: Content-Disposition Link ( , UTF-8):

js
var fileName = "my file(2).txt";
var header =
  "Content-Disposition: attachment; filename*=UTF-8''" +
  encodeRFC5987ValueChars(fileName);

console.log(header);
//  "Content-Disposition: attachment; filename*=UTF-8''my%20file%282%29.txt"

function encodeRFC5987ValueChars(str) {
  return (
    encodeURIComponent(str)
      // :  RFC3986  "!", RFC5987   ,       
      .replace(/['()]/g, escape) // i.e., %27 %28 %29
      .replace(/\*/g, "%2A")
      //        RFC5987,          : |`^
      .replace(/%(?:7C|60|5E)/g, unescape)
  );
}

Specification
ECMAScript 2027 LanguageSpecification
# sec-encodeuricomponent-uricomponent


Web Proxy Viewer  |  New URL  |  Original Page