百度移动版的url编码解码代码
时间:2014-04-30 20:11:33
收藏:0
阅读:590
1 var decode = function(m) { 2 try { 3 m = decodeURIComponent(m); 4 } catch(e) {} 5 var s = m.split("%"); 6 if (s.length > 1) { 7 s.shift(); 8 for(var i = 0; i < s.length; i++) { 9 var t = s[i]; 10 t = parseInt(t, 16); 11 t = t + 256; 12 t = t - 201; 13 t = t.toString(16); 14 s[i] = t; 15 } 16 m = ‘%‘+s.join(‘%‘); 17 return decodeURIComponent(m); 18 } else { 19 return m; 20 } 21 } 22 23 var encode = function(i) { 24 i = encodeURIComponent(i); 25 i = i.replace(/%(.{2})/gi, function(l) { 26 var m = l.replace("%", ""); 27 console.log(m); 28 m = parseInt(m, 16); 29 m = parseInt(‘201‘) + m; 30 m = m % 256; 31 m = m.toString(16); 32 if (m.length < 2) { 33 m = "0" + m 34 } 35 m = "%" + m; 36 return m 37 }); 38 return i; 39 }
评论(0)