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

Web design tips on form input boxes

This article lists some tips and codes about form...

Solution to the problem of MySQL deleting and inserting data very slowly

When a company developer executes an insert state...

Example of horizontal arrangement of li tags in HTMl

Most navigation bars are arranged horizontally as...

Nodejs module system source code analysis

Table of contents Overview CommonJS Specification...

WeChat applet implements the Record function

This article shares the specific code for the WeC...

How to install a virtual machine with Windows services on Mac

1. Download the virtual machine Official download...

Methods for optimizing Oracle database with large memory pages in Linux

Preface PC Server has developed to this day and h...

Ubuntu20.04 VNC installation and configuration implementation

VNC is a remote desktop protocol. Follow the inst...

JavaScript to implement the aircraft war game

This article shares with you how to use canvas an...

How to Delete Junk Files in Linux Elegantly

I wonder if you are like me, a programmer who arr...

How to install Elasticsearch7.6 cluster in docker and set password

Table of contents Some basic configuration About ...

Cleverly use CSS3's webkit-box-reflect to achieve various dynamic effects

In an article a long time ago, I talked about the...

The latest graphic tutorial of mysql 8.0.16 winx64 installation under win10

In order to download this database, it takes a lo...