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

How to use @media in mobile adaptive styles

General mobile phone style: @media all and (orien...

mysql5.7.19 winx64 decompressed version installation and configuration tutorial

Recorded the installation tutorial of mysql 5.7.1...

Vue uses vue meta info to set the title and meta information of each page

title: vue uses vue-meta-info to set the title an...

A set of code based on Vue-cli supports multiple projects

Table of contents Application Scenario Ideas Proj...

Detailed explanation of the background-position percentage principle

When I was helping someone adjust the code today,...

Explanation of Dockerfile instructions and basic structure

Using Dockerfile allows users to create custom im...

Detailed explanation of the pitfalls of mixing npm and cnpm

Table of contents cause reason Introduction to NP...

Causes and solutions for MySQL too many connections error

Table of contents Brief summary At noon today, th...

Sequence implementation method based on MySQL

The team replaced the new frame. All new business...

The difference between name and value in input tag

type is the control used for input and output in t...

jQuery plugin to implement floating menu

Learn a jQuery plugin every day - floating menu, ...

JS Asynchronous Stack Tracing: Why await is better than Promise

Overview The fundamental difference between async...

Mysql solves the database N+1 query problem

Introduction In orm frameworks, such as hibernate...

Dissecting the advantages of class over id when annotating HTML elements

There are very complex HTML structures in web pag...