Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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??

like image 960
þÍńķ Avatar asked Feb 05 '26 09:02

þÍńķ


2 Answers

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.

like image 196
Ionică Bizău Avatar answered Feb 06 '26 23:02

Ionică Bizău


Use the native Javascript function unescape, it is supported in all major browsers:

var a = "Name%20Name";
window.unescape(a);
like image 37
Jonathan Naguin Avatar answered Feb 06 '26 22:02

Jonathan Naguin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!