mozilla
Your Search Results

    encodeURIComponent()

    This translation is incomplete. Please help translate this article from English.

    概要

    特定の文字の実体を、UTF-8 文字エンコーディングで表された 1つ、2つ、あるいは、3つのエスケープシーケンスに置き換えることで、URI (Uniform Resource Identifier) をエンコードします。(サロゲートペアで構成される文字のみ4つのエスケープシーケンスになります)。

    構文

    encodeURIComponent(str);

    引数

    str
    URI の文字列

    説明

    encodeURIComponent は次を除く全ての文字をエスケープします。: 英数字, - _ . ! ~ * ' ( )

    上位・下位のペアでないサロゲート文字をエンコードしようとした場合 URIError が発生します。

    // 上位・下位の正しいペア
    console.log(encodeURIComponent('\uD800\uDFFF'));
    
    // 上位のみであり "URIError: malformed URI sequence" が発生
    console.log(encodeURIComponent('\uD800'));
    
    // 下位のみであり "URIError: malformed URI sequence" が発生
    console.log(encodeURIComponent('\uDFFF')); 
    

    To avoid unexpected requests to the server, you should call encodeURIComponent on any user-entered parameters that will be passed as part of a URI. For example, a user could type "Thyme &time=again" for a variable comment. Not using encodeURIComponent on this variable will give comment=Thyme%20&time=again. Note that the ampersand and the equal sign mark a new key and value pair. So instead of having a POST comment key equal to "Thyme &time=again", you have two POST keys, one equal to "Thyme " and another (time) equal to again.

    For application/x-www-form-urlencoded (POST), spaces are to be replaced by '+', so one may wish to follow a encodeURIComponent replacement with an additional replacement of "%20" with "+".

    To be more stringent in adhering to RFC 3986 (which reserves !, ', (, ), and *), even though these characters have no formalized URI delimiting uses, the following can be safely used:

    function fixedEncodeURIComponent (str) {
      return encodeURIComponent(str).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");
    }
    

    Examples

    The following example provides the special encoding required within UTF-8 Content-Disposition and Link server response header parameters (e.g., UTF-8 filenames):

    var fileName = 'my file(2).txt';
    var header = "Content-Disposition: attachment; filename*=UTF-8''" + encodeRFC5987ValueChars(fileName);
    
    console.log(header); 
    // logs "Content-Disposition: attachment; filename*=UTF-8''my%20file%282%29.txt"
    
    
    function encodeRFC5987ValueChars (str) {
        return encodeURIComponent(str).
            // Note that although RFC3986 reserves "!", RFC5987 does not,
            // so we do not need to escape it
            replace(/['()]/g, escape). // i.e., %27 %28 %29
            replace(/\*/g, '%2A').
                // The following are not required for percent-encoding per RFC5987, 
                //  so we can allow for a little better readability over the wire: |`^
                replace(/%(?:7C|60|5E)/g, unescape);
    }
    

    Specifications

    Specification Status Comment
    ECMAScript 3rd Edition. Standard Initial definition.
    ECMAScript 5.1 (ECMA-262)
    The definition of 'encodeURIComponent' in that specification.
    Standard  
    ECMAScript 6 (ECMA-262)
    The definition of 'encodeURIComponent' in that specification.
    勧告候補  

    Browser compatibility

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
    Basic support (有) (有) (有) (有) (有)
    Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
    Basic support (有) (有) (有) (有) (有) (有)

    See also

     

    ドキュメントのタグと貢献者

    Contributors to this page: SphinxKnight, Jxck, ethertank, s_fujimoto, ssw, teoli, Hot, Potappo
    最終更新者: teoli,
    サイドバーを隠す