Regarding the Chinese garbled characters in a href parameter transfer

Regarding the Chinese garbled characters in a href parameter transfer

When href is needed to pass parameters, and the parameters are in Chinese, garbled characters will appear. The simplest method is:

The passed value is first encrypted with escape() , and then decrypted with unescape() on the value access page. It has been tested and is effective.

I checked online and found that urlencode can be used for encryption and urldecode can be used for decryption.

Um... what is the difference between the two? I just checked and sorted it out, source (http://www.cnblogs.com/glory-jzx/archive/2013/06/14/3135580.html):

js involves 3 functions to encode text : escape, encodeURI, encodeURIComponent, and 3 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")+">Quit< /a>');</script>

>2. When redirecting URL, you can use encodeURI as a whole
For example: Location.href=encodeURI("http://cang.baidu.com/do/s?word=百度&ct=21");

3. Escape can be used when js uses data
For example: history records in collection.

4. When escape encodes unicode values ​​other than 0-255, the output is in %u**** format. In other cases, the encoding results of escape, encodeURI, and encodeURIComponent are the same.

5. The most commonly used one should be encodeURIComponent , which converts special characters such as Chinese and Korean into utf-8 URL encoding. Therefore, if you need to use encodeURIComponent to pass parameters to the background, the background decoding needs to support utf-8 (the encoding method in the form is the same as the encoding method of the current page)

6. No encoding:

There are 69 escape characters: *, +, -, ., /, @, _, 0-9, az, AZ

There are 82 characters that are not encoded in encodeURI: !, #, $, &, ', (,), *, +, ,, -, ., /, :, ;, =, ?, @, _, ~, 0-9, az, AZ

encodeURIComponent does not encode 71 characters: !, ', (,), *, -, ., _, ~, 0-9, az, AZ

What is the difference between escape() and encodeURI()?

escape() method:
All spaces, punctuation marks, accented characters, and any other non-ASCII characters are encoded as "%XX", where xx is a hexadecimal number.
The escape and unescape encoding and decoding functions, escape returns a hexadecimal encoding of an ISO Latin character set . The unescape function converts a hexadecimal encoding with special values ​​into an ASCII string
Example:
escape('!@#$%^&*(){}[]=:/;?+\'"'):
Result:%21@%23%24%25%5E%26*%28%29%7B%7D%5B%5D%3D%3A/%3B%3F+%27%22

encodeURI() Method
The Encodeuri method returns an encoded URI . Therefore, if you use the Decodeuri method on the result, the original string will be returned. The Encodeuri method does not encode the following characters: ", "/", ";", "?". However, you can use the EncodeuriComponent method to encode these characters.
encodes, a Uniform Resource Identifier (URI) (URI) replaces certain characters one by one, describing the characteristics of UTF-8 encoding.
For example:
encodeURI('!@#$%^&*(){}[]=:/;?+\'"'):
Result:!@#$%25%5E&*()%7B%7D%5B%5D=:/;?+'%22

encodeURIComponent() method:
The encodeuricomponent method returns an encoded URI. So if you call decodeuricomponent, the original string will be returned. Since all text is encoded by the encodeuricomponent method, be careful if there are paths in the string such as: "/FOLDER1/FOLDER2/DEFAULT.HTML". The encoding will not work and will fail if used as a request to a web server. Use the Encodeuri method when the string is composed of more than one URI.
Example: The easiest way is to look at the code they generate after encrypting these characters.
encodeURIComponent('!@#$%^&*(){}[]=:/;?+\'"'):
Results!%40%23%24%25%5E%26*()%7B%7D%5B%5D%3D%3A%2F%3B%3F%2B'%22

[When is it appropriate to use which method?]

The escape() method does not encrypt + it will be interpreted as spaces on the server side and in form fields. Due to this shortening, you should avoid using this method if possible. If you have to choose between the two, it is best to always use encodeURIComponent().

escape() does not encrypt: @*/+

encodeURI() is slightly more specialized than escape(), and is used to encode URIs. One is the opposite of a querystring, which is a part of a URL. Use this method when you need to convert a string into a URI resource identifier and need certain characters to remain unencoded. Remember, the ' character is not encoded because it is included in the URI.

encodeURI() will not encrypt: !@#$&*()=:/;?+'

Finally, the encodeURIComponent() method is used in most cases when you need to encode a single URI component. This method can encode certain characters that are special to URIs so that most components can be included. Remember, the ' character itself is included in URIs, so this method will not encode it.

encodeURIComponent() does not encrypt: !*()'
http://shijian0306.javaeye.com/blog/241264

I have only used escape, so this is for reference only.

The above article about the Chinese garbled code problem in a href parameter passing is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

Original URL: http://www.cnblogs.com/zhangym118/archive/2016/07/05/5644915.html

<<:  Vue+Echart bar chart realizes epidemic data statistics

>>:  HTML imitates Baidu Encyclopedia navigation drop-down menu function

Recommend

Detailed explanation of how to select all child elements using CSS

How to recursively select all child elements usin...

Installation steps of Ubuntu 20.04 double pinyin input method

1. Set up Chinese input method 2. Set the double ...

Difference and implementation of JavaScript anti-shake and throttling

Table of contents 1. Anti-shake 2. Throttling 3. ...

How to Use rsync in Linux

Table of contents 1. Introduction 2. Installation...

Analysis of CocosCreator's new resource management system

Table of contents 1. Resources and Construction 1...

Teach you how to install docker on windows 10 home edition

When I wrote the Redis book and the Spring Cloud ...

CSS form validation function implementation code

Rendering principle In the form element, there is...

Detailed explanation of Linux CPU load and CPU utilization

CPU Load and CPU Utilization Both of these can re...

Detailed explanation of MySQL EXPLAIN output columns

1. Introduction The EXPLAIN statement provides in...