Compare Revisions

encodeURIComponent()

Revision 432505:

Revision 432505 by Brettz9 on

Revision 432531:

Revision 432531 by Brettz9 on

Title:
encodeURIComponent
encodeURIComponent
Slug:
Web/JavaScript/Reference/Global_Objects/encodeURIComponent
Web/JavaScript/Reference/Global_Objects/encodeURIComponent
Content:

Revision 432505
Revision 432531
tt85    <h2 name="See_also">
86      Examples
87    </h2>
88    <p>
89      The following example provides the special encoding require
 >d within UTF-8 <code>Content-Disposition</code> and <code>Link</c
 >ode> server response header parameters (e.g., UTF-8 filenames):
90    </p>
91    <pre class="brush: js" name="See_also">
92var fileName = 'my file(2).txt';
93var header = "Content-Disposition: attachment; filename*=UTF-8''"
 > + encodeRFC5987ValueChars(fileName);
94alert(header); // Content-Disposition: attachment; filename*=UTF-
 >8''my%20file%282%29.txt
95 
96 
97function encodeRFC5987ValueChars (str) {
98    return encodeURIComponent(str).
99        // Note that although RFC3986 reserves "!", RFC5987 does 
 >not, so we do not need to escape it
100        replace(/['()]/g, escape). // i.e., %27 %28 %29
101        replace(/\*/g, '%2A').
102            // The following are not required for percent-encodin
 >g per RFC5987, so we can allow for a little better readability ov
 >er the wire: |`^
103            replace(/%(?:7C|60|5E)/g, unescape);
104}
105</pre>

Back to History