The solution of URL passing parameters (special characters) in JavaScript and the introduction of transcoding decoding some symbols cannot be passed directly in the URL, and if you want to pass these special symbols in the URL, use their encoding. Some URL special symbols and codes are listed in the following table.
Hexadecimal value
1. + URL + sign for space%2b
2. Spaces in the space URL can be used with the + number or code%20
3./separate directories and subdirectories%2f
4.? Separating the actual URLs and parameters%3f
5.% Specify special characters%25
6. # means bookmark%23
7. The delimiter between the parameters specified in the & URL%26
8. = value of the specified parameter in the URL%3d
The workaround:
Replace () method if directly with Str.replace ("-", "!") Only the first matching character is replaced.
and Str.replace (/\-/g, "!") You can replace all matching characters (G is the global flag).
Replace ()
The substitution character variable in JS is as follows:
Data2=data2.replace (/\%/g, "%25");
Data2=data2.replace (/\#/g, "%23");
Data2=data2.replace (/\&/g, "%26");
When using URLs for parameter passing, some parameters or URL addresses with Chinese names (or special characters) are often passed, and conversion errors occur during background processing. In some pass-through pages use GB2312, and the Receive page uses UTF8, so that the received parameters may be inconsistent with the original. URLs that are encoded using the server-side UrlEncode function are not the same as URLs encoded with the encodeURI function of the client java.
Java encodes text in 3 functions: Escape,encodeuri,encodeuricomponent, corresponding 3 decoding functions: Unescape,decodeuri,decodeuricomponent
Coding methods in Java:
Escape () Method: encodes the specified string using the ISO Latin character set. All space characters, punctuation marks, special characters, and other non-ASCII characters are converted to the character encoding in the%xx format (XX equals the encoded 16-digit number of the character in the character set table). For example, the encoding for a space character is%20. The Unescape method is the opposite. Characters that are not encoded by this method: @ */+
encodeURI () Method: Converts the URI string into an escape format using the UTF-8 encoding format. Characters that will not be encoded by this method:! @ # $& * () =:/;? +
encodeURIComponent () Method: Converts the URI string into an escape format using the UTF-8 encoding format. Compared to encodeURI (), this method encodes more characters, such as characters. So if the string contains several parts of the URI, it cannot be encoded in this way, otherwise the URL will display an error after the/character is encoded. Characters that will not be encoded by this method:! * ( )
Therefore, for the Chinese string, if you do not want to convert the string encoding format into UTF-8 format (such as the original page and the target page charset is consistent), only need to use escape. If your page is GB2312 or other encoding, and the page that accepts the parameter is UTF-8 encoded, it is necessary to use encodeURI or encodeuricomponent.
In addition, Encodeuri/encodeuricomponent was introduced after java1.5, and escape is available in the java1.0 version.
1, passing parameters need to use encodeuricomponent, so that the combined URL will not be # and other special characters truncated.
For example:HTML code
- < languagelanguage= "Java" >write (' <a href= ' http://passport.baidu.com/?logout&aid=7&u= ' + encodeURIComponent ("Http://cang.baidu.com/bruce42") + ' > Exit </a> ');</>
2, the URL to jump when you can use the whole encodeURI
For example:Java code
- Location.href=encodeuri ("http://cang.baidu.com/do/s?word= Baidu &ct=21");
3, JS can use escape when using data,
For example: Search the history record in Tibet.
4, escape output%u**** format when encoding Unicode values other than 0-255, escape,encodeuri,encodeuricomponent encoding results are the same in other cases.
is used up to encodeURIComponent, which is a URL encoding that converts special characters such as Chinese, Korean, and so on to utf-8 format. Therefore, if you need to use encodeURIComponent to pass parameters to the background, you need the background decoding for UTF-8 support (the encoding in form is the same as the current page encoding)
Escape does not encode 69 characters: *,+,-,.,/,@,_ , 0-9,a-z,a-z
encodeURI does not encode 82 characters:!,#,$,&, ', (,), *,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,a-z
encodeURIComponent does not encode 71 characters:!, ', (,), *,-,.,_,~,0-9,a-z,a-z
The following is a special word that may be used in the URL indicators the encoded value in the URL:
Character the meaning of
Special characters
URL encoding
Symbol |
Explain |
Escape |
# |
Used to flag a specific document location |
%23 |
% |
Encode a special character |
%25 |
& |
Separating different value pairs of variables |
%26 |
+ |
Spaces are represented in variable values |
%2b |
/ |
Represents a directory path |
%2f |
\ |
Represents a directory path |
%5c |
= |
Used to connect keys and values |
%3d |
? |
Represents the start of a query string |
%3f |
Space |
Space |
%20 |
. |
Period |
%2e |
: |
Colon |
%3a |
The project found that directly to the parameter part of the URL do encodeURI () encoding conversion, background servlet through Getparamater (), do not need the conversion can be directly obtained to the correct value.
Appendix:Encoding and decoding in JavaScript
The encoding and decoding functions available in JavaScript are grouped as follows:
- Escape (String);
Unescape (string);
- encodeURI (string);
decodeURI (string);
- encodeURIComponent (string);
decodeURIComponent (string);
The difference between them is:
Escape/unescape:
A 16 encoding string, the space, symbols and other characters with%XX encoding, the Chinese characters are expressed in%uxxxx encoding. Since javascript1.5, this method has not been recommended for use.
Encodeuri/decodeuri:
Encode strings in UTF-8 encoding, on these characters: " ;,/?: @ & = + $ " does not encode.
Encodeuricomponent/decodeuricomponent:
Encodes all strings in UTF-8 encoding.
Because Escape/unescape has been deprecated. Do not say it, encodeURI and encodeuricomponent before the difference with the example description:
For example, to use a Get method to pass a parameter u to the server:
varU= "Index.php?blogid=1&op=default";
varGeturl= "http://www.simplelife.cn/test.php?p=" +encodeuri (U);
Here, if encodeURI is used, then the value of the final Geturl is:
Http://www.simplelife.cn/test.php?p=index.php?blogId=1&op=Default
In this way, the character "&op=default" in the parameter u will not be passed as a string parameter to the server side, but rather as a test.php parameter, because the character "&" in "&op=default" is not encoded.
Therefore, in this scenario, you need to use encodeURIComponent, the encoded Geturl value is:
Http://www.simplelife.cn/test.php?p=index.php%3FblogId%3D1%26op%3DDefault
In this way, the parameters can be passed smoothly. The string that gets on the server side will be the correct U.
Conversely, if you need to get access to a URL, but the URL contains Chinese characters, in order to prevent garbled and other encoding problems, you need to encode the URL through encodeURI. Reprint: https://www.cnblogs.com/Tracy-zdy/p/3822080.html
Front-end URL passing encoding problem