How to deal with Chinese garbled characters of url webpage addresses in js

Source: Internet
Author: User

Act = exercises & kd = 3258 & kname = % E4 % B8 % 8D % E7 % AD % 89% E5 % BC % 8F % E7 % 9A % 84% E8 % A7 % A3 % E6 % B3 % 95% EF % BC % 88% E5 % 88% E6 % 9E % 86% E6 % B3 % 90% E3 % 95% E7 % BB % BC % E5 % 80% E6 % B3 % 95% E3 % 80% E6 % AF % 81% E8 % BE % 94% E6 % B3 % 83% EF % BC % 89.
This is because the browser encodes the url path by default. In addition, the url encoding varies with different browsers.
This is just a representation. This is not the problem. The problem is that when we get the kname value, we get a bunch of messy things. In this way, we cannot perform subsequent operations in the program.
What should we do?
Now I will tell you how to solve the problem. Maybe my solution cannot solve the problem you encountered, but at least it can solve the problem I encountered. Let's write it out and share it with you:
1. First, transcoding is performed when the address is transferred:
In my code:
[Html]
<A href = "http://www.17xmf.com/index.php? Act = exercises & kd = 22 & kname = encodeURI (my exams) "> my exams </a>
2. At the receiving end, my receiving end is js, so I will use the js method. If it is received in php or other languages, I will find the online materials. However, the idea should not be changed. Instead, the processing function should use php.
[Html]
Var kname = request ('kname ');
Kname = decodeURI (kname );
 
/**
* @ Author gayayang
* @ Date 2012-9-18
* @ Todo: Obtain the get parameter of the url.
* @ Param url
* @ Returns
*/
Function request (paras ){
Var url = location. href;
Var paraString = url. substring (url. indexOf ("? ") + 1, url. length). split ("&");
Var paraObj = {};
For (var I = 0; j = paraString [I]; I ++ ){
ParaObj [j. substring (0, j. indexOf ("= ")). toLowerCase ()] = j. substring (j. indexOf ("=") + 1, j. length );
}
Var returnValue = paraObj [paras. toLowerCase ()];
If (typeof (returnValue) = "undefined "){
Return "";
} Else {
Return returnValue;
}
}
The request method is the method of getting the parameters in the address I wrote. The decodeURI () method is to decode the Chinese characters that the transmitter intentionally transcodes. DecodeURI () and encodeURI () methods use one method at a time, but they cannot be reversed.
However, the above method is not compatible with IE. It can only be solved by Firefox, and ie will be displayed as: encodeURI (my exam ). What should we do? Well, give the answer:
Do not use the encodeURI () function on the transfer end. The rest remains unchanged. That's simple.
That is OK! That's simple.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.