Search code examples
jqueryquery-stringquerystringparameter

How to remove spaces from a query string using Jquery?


Iam using query sting in jquery

to get URL values iam using

    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;

Here when i pass Query String to URL it is giving spaces as %20 and when i fectch value from URL iam getting vaue of name as Name%20Name format

What should i do in order to get name with space as seperation??


Solution

  • You need the decodeURIComponent() Javascript function:

    decodeURIComponent("Name%20Name") // "Name Name"
    

    Decodes a Uniform Resource Identifier (URI) component previously created by encodeURIComponent or by a similar routine.

    For more information see the documentation.