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 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.- 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
encodeURIComponent
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.- 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
- 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
<strong style="border: 0px; padding: 0px;">%u</strong><em style="border: 0px; padding: 0px;">nnnn</em>
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.
(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 |
相关推荐
hanxia 2020-07-05
typhoonpython 2019-11-13
Eric0Lv 2019-04-02
mrsuddenflash 2012-04-25
Gcalolin 2019-06-27
Yellowpython 2019-06-20
CsdnGame 2012-05-17
AJAXBloger 2011-07-22
史林峰的个人 2016-10-30
cakecc00 2010-03-11
outsiderlcy 2015-03-18
红薯藤 2019-04-20
wangtengphp 2019-04-16
weixuejunphp 2019-04-15
phpyounger 2019-04-12
phpsir 2019-04-11
jackm 2010-07-23
加油码农 2010-07-21