escape,encodeURI,encodeURIComponent

js involves three functions to encode text: escape, encodeURI, encodeURIComponent, and three corresponding decoding functions: unescape, decodeURI, decodeURIComponent
1. When passing parameters, you need to use encodeURIComponent, so that the combined url will not be truncated by special characters such as #.
For example: <script language="javascript">document.write('<a href="http://passport.baidu.com/?logout&aid=7&u='+encodeURIComponent("http://cang.baidu.com/ bruce42")+'">exit</a>');</script>
2. EncodeURI can be used as a whole when url jumping,
for example: Location.href=encodeURI("http://cang.baidu.com/do /s?word=Baidu&ct=21");
3. When js uses data, you can use escape
[Huoho.
Com editor] For example: the history records in the collection.
4. When escape encodes unicode values ​​other than 0-255, it outputs %u**** format. In other cases, escape, encodeURI, and encodeURIComponent have the same encoding result.
The most used should be encodeURIComponent, which is url encoding that converts special characters such as Chinese and Korean into utf-8 format, so if you need to use encodeURIComponent to pass parameters to the background, you need background decoding to support utf-8 (the encoding method in the form). Same as the current page encoding)
escape does not encode 69 characters: *, +, -, ., /, @, _, 0-9, az, AZ
encodeURI does not encode 82 characters: !, #, $, &, ', (,) ,*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,az,AZ
encodeURIComponent does not encode 71 characters: !, ', (,), *, -, ., _, ~, 0-9, az, AZ
(the codes corresponding to uncoded characters are: !< %21 >'<%27>(<%28>)<%29>*<% 2a>-<%2d>.<%2e>_< %5f >~<%7e>)
Below is the usual code % 20Space %21%22 \" %23 # %24 $ %25 % %26%27 ' %28%29 ) %2a * %2b + %2c , %2d - %2e .%2f /
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326954652&siteId=291194637