Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing %20 from URL parameters

Tags:

javascript

I know you need to use some variant of decodeURIComponent() to do something like this, but since I'm still fairly new to coding and using some code I found on the net for my purposes, I'm not sure how to go about changing it to suit my needs.

What I have is a function that gets each URL parameter I need out of the URL (of which there are many). I have to use these variables for other functions as parameters and also to display on the page, and I can't get the %20's to disappear.

function getUrlVars() {
            var vars = {};
            parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
            vars[key] = value;
            });
            return vars;
        }

Where I get each variable using:

var markname = getUrlVars()["mname"];

I've tried to put decodeURIComponent() in different places in that function, but I can't seem to get it to work. I'm also not sure if it needs to use value or vars.

value = decodeURIComponent(value);

Or something like that...

Any help would be appreciated! Thanks!

like image 219
Matthew Paxman Avatar asked Feb 27 '13 00:02

Matthew Paxman


People also ask

How do you remove something from a URL?

To request removal of a directory or site, click on the site in question, then go to Site configuration > Crawler access > Remove URL. If you enter the root of your site as the URL you want to remove, you'll be asked to confirm that you want to remove the entire site.

How do you exclude a URL in a query parameter?

One way to remove query parameters from pages is through the View Settings. Under Admin > View Settings > Exclude Query Parameters, list the query parameters that you want to exclude from your page paths.

How do you separate parameters in a URL?

URL parameters are made of a key and a value, separated by an equal sign (=). Multiple parameters are each then separated by an ampersand (&).


1 Answers

decodeURIComponent as you posted should work fine. You might as well replace plus signs with spaces, and don't forget to decode the key as well:

function getUrlVars() {
    var url = window.location.href,
        vars = {};
    url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
         key = decodeURIComponent(key);
         value = decodeURIComponent(value);
         vars[key] = value;
    });
    return vars;
}
like image 167
Bergi Avatar answered Sep 30 '22 19:09

Bergi