When js gets the parameters in the url and the parameters are in Chinese it is decoded by js

  • 2020-03-30 02:25:22
  • OfStack

If the parameter passed is:
<a href="${pageContext.request.contextPath}/productdisplay/productDisplay_productDisplayUI.action?pkId=${pkId}&name=${name}" style="color:white; margin-top:10px; margin-bottom:10px;">${name}</a> 

The js to get the url parameter is as follows:
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[hash[0]] = hash[1]; 
return vars; 

If there is Chinese in the url, there will be Chinese garbled code when taking parameters, decodeURI() method is used to decode, as follows:
//Gets the pkId of the category to which it belongs
var params = getUrlVars(); 
//Gets the id for this category
var parentId = params[params[0]]; 
//Gets the name of the category
var productName = decodeURI(params[params[1]]); 

Related articles: