arsenal
arsenal

Reputation: 24154

Remove the spaces after extract the url parameters

I am trying to extract the url parameters using Javascript/HTML.

Below is the code, I have and its working fine if I don't give any spaces in between the parameter.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>

    </head>
    <body>
        <script>
                function getUrlVars() {
        var vars = {};
        var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
        });
        return vars;
        }
            var first = getUrlVars()["id"];

            alert(first);

        </script>
    </body>
</html>

But as soon as I give spaces in between the parameter, I always get %20 in between. How to skip the spaces.

Suppose if below is the url-

/test.html?id=123 456

then in the alert box I always see as-

123%20456

How can I avoid the spaces using the above regex I have in the code.

Upvotes: 0

Views: 1719

Answers (1)

taylorc93
taylorc93

Reputation: 3716

If you don't want the URL encoding (%20) to appear, you can use the unescape() function.

var first = unescape(first);

I would recommend using better variable names, but just use this wherever you are encountering problems with %20 showing up and it will convert it to a plain space

Upvotes: 3

Related Questions