欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

百度移动版的url编码解码示例_javascript技巧

程序员文章站 2022-05-08 11:37:03
...
复制代码 代码如下:

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 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 m = "0" + m
}
m = "%" + m;
return m
});
return i;
}