Home >Web Front-end >JS Tutorial >Baidu mobile version url encoding and decoding example_javascript skills

Baidu mobile version url encoding and decoding example_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:50:421849browse
复制代码 代码如下:

var decode = function(m) {
try {
m = decodeURIComponent(m);
} catch(e) {}
var s = m.split("%");
if (s.length > 1) {
s.shift();
for(var i = 0; i < s.length; i ) {
var t = s[i];
t = parseInt(t, 16);
t = t 256;
t = t - 201;
t = t.toString(16);
s[i] = t;
}
m = '%' s.join('%');
return decodeURIComponent(m);
} else {
return m;
}
}

var encode = function(i) {
i = encodeURIComponent(i);
i = i.replace(/%(.{2})/gi, function(l) {
var m = l.replace("%", "");
console.log(m);
m = parseInt(m, 16);
m = parseInt('201') m;
m = m % 256;
m = m.toString(16);
if (m.length < 2) {
m = "0" m
}
m = "%" m;
return m
});
return i;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn