Home >Web Front-end >JS Tutorial >js gets the parameters in the url and decodes them through js when the parameters are Chinese_javascript skills

js gets the parameters in the url and decodes them through js when the parameters are Chinese_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:55:121346browse

If the parameters passed are:

Copy code The code is as follows:

The js for getting the url parameters is as follows:
Copy code The code is as follows:

/**Initial loading completed*/
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;
}

If there is Chinese in the url, there will be Chinese garbled characters when taking the parameters. You have to use the decodeURI() method to decode, as follows:
Copy code The code is as follows:

//Get the pkId of the category
var params = getUrlVars();
//Get the id of this category
var parentId = params[params[0]];
//Get the name of the category
var productName = decodeURI(params[params[1]]);
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