最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Removing %20 from URL parameters - Stack Overflow

programmeradmin1浏览0评论

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!

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!

Share Improve this question asked Feb 27, 2013 at 0:43 Matthew PaxmanMatthew Paxman 2472 gold badges4 silver badges13 bronze badges 5
  • What is an example value that is wrong (that you want decoded)? – Explosion Pills Commented Feb 27, 2013 at 0:44
  • 2 decodeURIComponent() is what you are looking for. Can you provide an example of what exactly went wrong? – corylulu Commented Feb 27, 2013 at 0:46
  • My URL looks something like this: ?mapid=1h2pwR4ULN3GHAENYtww-gINXMgvxAaHBu5waliI&mapname=test&jnumber=1234&updated=01/01/01&marker=3&mname=Driver%20Reviver%20Locations So something like Driver Reviver Locations would need to be displayed on screen, and is appearing as Driver%20Reviver%20Locations. In some cases as well, these variables need to be plugged in to functions, and I'm worried if I don't curb all the %anything that eventually it'll screw up on itself. – Matthew Paxman Commented Feb 27, 2013 at 0:55
  • You might want to have a look at this answer for a 100% working solution (or the whole question for an overview) – Bergi Commented Feb 27, 2013 at 0:57
  • Does this answer your question? How can I get query string values in JavaScript? – Heretic Monkey Commented Jul 18, 2021 at 21:42
Add a ment  | 

3 Answers 3

Reset to default 6

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;
}

when you pass the url use str_replace(" ","-",$name) and decode it by str_replace("-"," ",$p->property_name)

it will remove space and add - in the url

simply do this ("your string here").replaceAll(" ", "-")

发布评论

评论列表(0)

  1. 暂无评论