converting strings to gb2312 or utf-8 encoded parameters (JS version) _javascript tips

Source: Internet
Author: User
In many cases, we directly in the URL to pass Chinese parameters, read the Chinese are garbled, then how should we convert these parameters?

Now let's introduce the method.
1, we create a new urlencode.js and then copy the following code into the
Copy Code code as follows:

JS version of the Server.URLEncode encoding function
String.prototype.UrlEncodeGB2312 = function () {
var str = this;
str = str.replace (/./g, function (ShEx) {
Window. Encodestr = "";
Window.shex = ShEx;
Window.execscript (' window. Encodestr=hex (ASC (Window.shex)) ', "VBScript");
return window. Encodestr.replace (/.. /g, "%{blogcontent}amp;");
});
return str;
}
String.prototype.UrlEncode = function () {
var s = Escape (this);
var sa = s.split ("%");
var Retv = "", RetE = "";
if (Sa[0]!= "") {
Retv = sa[0];
}
for (var i = 1; i < sa.length; i++) {
if (sa[i].substring (0, 1) = = "U") {
Retv + = Hex2utf8 (Str2hex (sa[i].substring (1, 5));
if (Sa[i].length > 4)
Retv + + sa[i].substring (5);
}
else Retv + = "%" + sa[i];
}
return RETV;
}
function Str2hex (s) {
var c = "";
var n;
var ss = "0123456789ABCDEF";
var digs = "";
for (var i = 0; i < s.length; i++) {
c = S.charat (i);
n = ss.indexof (c);
Digs + = Dec2dig (eval (n));
}
return digs;
}
function Dec2dig (N1) {
var s = "";
var n2 = 0;
for (var i = 0; i < 4; i++) {
N2 = Math.pow (2, 3–i);
if (N1 >= n2) {
s + + ' 1 ';
N1 = N1–N2;
}
Else
s + + ' 0 ';
}
return s;
}
function Dig2dec (s) {
var Retv = 0;
if (s.length = = 4) {
for (var i = 0; i < 4; i++) {
Retv + = eval (S.charat (i)) * MATH.POW (2, 3–i);
}
return RETV;
}
return-1;
}
function Hex2utf8 (s) {
var RetS = "";
var TempS = "";
var ss = "";
if (s.length = = 16) {
TempS = "1110" + s.substring (0, 4);
TempS + = "Ten" + s.substring (4, 10);
TempS + = "Ten" + s.substring (10, 16);
var sss = "0123456789ABCDEF";
for (var i = 0; i < 3; i++) {
RetS + = "%";
SS = temps.substring (I * 8, (eval (i) + 1) * 8);
RetS + = Sss.charat (Dig2dec (ss.substring (0, 4));
RetS + = Sss.charat (Dig2dec (ss.substring (4, 8));
}
return RetS;
}
Return "";
}

2, the use of the method, of course, is our ((string). UrlEncode ()) can convert a string to a utf-8-encoded URL parameter (string. UrlEncodeGB2312 ()) You can convert the string into gb2312 encoded parameters, well, O (∩_∩) o haha ~

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.