Blovesea 2014-01-07
The encodeURI, encodeURIComponent and escape functions convert special characters in URLs and other URIs by percent encoding the special characters.
JavaScript: encodeURI functionThe JavaScriptencodeURI
function is used to encode an entire unencoded URI, such as http://[email protected]/my path/my filename.ext?width=100%&my key=my value#fragment-id
. It is most useful when the entire URI is hard-coded in the JavaScript code, so that escaping of special characters within any component is already done manually.#& ./:=?@
that act as delimiters within a URI along with $;
and all other characters not encoded by encodeURIComponentencodeURIComponent
function can be used to encode individual components of a URI such as http
, authority
, www.ExampleOnly.com
, my path
, my filename.ext
, width
, 100%
, my key
, my value
and fragment-id
from the example used for encodeURI above. This is the function that should be used when the URI is being constructed from variables containing individual components of the URI.#$& ,/:;=?@
within a component, in addition to those encoded by the encodeURI function, so they won't be misinterpreted as URI delimiters!'()*-._~
, which are considered "safe" by RFC 1738, but does encode the characters $ ,
anyway+
) or that was designed to be compatible with older browsers<strong style="border: 0px; padding: 0px;">%u</strong><em style="border: 0px; padding: 0px;">nnnn</em>
rather than using UTF-8 percent escape codesIn all cases, the resulting URI still needs to be converted to valid HTML, by encoding quotes within attributes, ampersands, etc. using HTML character codes.
(space) | %20 | %20 | %20 | %20 |
! | ! | ! | ! | %21 |
" | %22 | %22 | %22 | %22 |
# | # | # | %23 | %23 |
$ | $ | $ | %24 | %24 |
% | %25 | %25 | %25 | %25 |
& | & | & | %26 | %26 |
' | ' | ' | ' | %27 |
( | ( | ( | ( | %28 |
) | ) | ) | ) | %29 |
* | * | * | * | * |
(space) | %20 | %20 | %20 | %20 |
, | , | , | %2C | %2C |
- | - | - | - | - |
. | . | . | . | . |
/ | / | / | %2F | / |
: | : | : | %3A | %3A |
; | ; | ; | %3B | %3B |
< | %3C | %3C | %3C | %3C |
= | = | = | %3D | %3D |
> | %3E | %3E | %3E | %3E |
? | ? | ? | %3F | %3F |
@ | @ | @ | %40 | @ |
[ | %5B | %5B | %5B | %5B |
\ | %5C | %5C | %5C | %5C |
] | %5D | %5D | %5D | %5D |
^ | %5E | %5E | %5E | %5E |
_ | _ | _ | _ | _ |
` | %60 | %60 | %60 | %60 |
{ | %7B | %7B | %7B | %7B |
| | %7C | %7C | %7C | %7C |
} | %7D | %7D | %7D | %7D |
~ | ~ | ~ | ~ | %7E |
© | %C2%A9 | %C2%A9 | %C2%A9 | %A9 |
® | %C2%AE | %C2%AE | %C2%AE | %AE |
— | %E2%80%94 | %E2%80%94 | %E2%80%94 | %u2014 |
™ | %E2%84%A2 | %E2%84%A2 | %E2%84%A2 | %u2122 |