JavaScript URL解码function

什么是最好的JavaScripturl解码工具? 编码也不错,并与jQuery的工作是一个额外的好处。

我也使用了encodeURIComponent()和decodeURIComponent() 。

这是一个完整的function(取自PHPJS ):

 function urldecode(str) { return decodeURIComponent((str+'').replace(/\+/g, '%20')); } 
 decodeURIComponent(mystring); 

你可以通过使用这一位代码来获得传递的参数:

 //parse URL to get values: var i = getUrlVars()["i"]; function getUrlVars() { var vars = [], hash; var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); for (var i = 0; i < hashes.length; i++) { hash = hashes[i].split('='); vars.push(hash[0]); vars[hash[0]] = hash[1]; } return vars; } 

或者这一行就可以得到参数:

 location.search.split("your_parameter=")[1] 

用这个

 unescape(str); 

我不是一个伟大的JS程序员,尝试了所有,这很棒!

 //How decodeURIComponent Works function proURIDecoder(val) { val=val.replace(/\+/g, '%20'); var str=val.split("%"); var cval=str[0]; for (var i=1;i<str.length;i++) { cval+=String.fromCharCode(parseInt(str[i].substring(0,2),16))+str[i].substring(2); } return cval; } document.write(proURIDecoder(window.location.href)); 

如果您负责使用urlencode在PHP中对数据进行编码,则PHP的rawurlencode可以与JavaScript的decodeURIComponent一起使用,而不需要replace+字符。

这是我用的:

在JavaScript中:

 var url = "http://www.mynewsfeed.com/articles/index.php?id=17"; var encoded_url = encodeURIComponent(url); var decoded_url = decodeURIComponent(encoded_url); 

在PHP中:

 $url = "http://www.mynewsfeed.com/articles/index.php?id=17"; $encoded_url = url_encode(url); $decoded_url = url_decode($encoded_url); 

你也可以在网上尝试: http : //www.mynewsfeed.x10.mx/articles/index.php?id=17