ZzJavaScript encode and escape functions


 

JavaScript encode and escape functions

The encodeURI, encodeURIComponent and escape functions convert special characters in URLs and other URIs by percent encoding the special characters.

JavaScript: encodeURI  function
The  JavaScript encodeURI 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.
  • encodes all characters that should never be included in a valid URI
  • also encodes any percent signs, which is used to encode the unsafe characters
  • leaves intact the special characters #& ./:=?@ that act as delimiters within a URI along with $; and all other characters not encoded by encodeURIComponent
  • uses percent escape encoding of individual UTF-8 octets for non-ASCII characters
JavaScript: encodeURIComponent  function
The  JavaScript encodeURIComponent function can be used to encode individual components of a URI such as  httpauthoritywww.ExampleOnly.commy pathmy filename.extwidth100%my keymy value and  fragment-idfrom 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.
  • encodes the special characters #$& ,/:;=?@ within a component, in addition to those encoded by the encodeURI function, so they won't be misinterpreted as URI delimiters
  • leaves intact the alphanumeric characters and special characters !'()*-._~, which are considered "safe" by RFC 1738, but does encode the characters $ , anyway
  • uses percent escape encoding of individual UTF-8 octets for non-ASCII characters
JavaScript: escape  function
  • The JavaScript escape function should be avoided but may be seen in older code that encodes a space as a plus sign (+) or that was designed to be compatible with older browsers
  • should not be used for text that may contain non-ASCII characters because Unicode characters are converted into a non-standard format as %unnnn rather than using UTF-8 percent escape codes
JavaScript percent-encoding functions

In all cases, the resulting URI still needs to be converted to valid HTML, by encoding quotes within attributes, ampersands, etc. using HTML character codes.

Character encodeURI HTML encodeURIComponent escape
         
(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

你可能感兴趣的:(JavaScript,escape,encodeURI)