4

I know you can obtain an URL variable by calling getUrlVars()["id"], however is there a way to get all (an unknown number of) variables in the URL? For a few reasons I am only allowed to do this on client side.

2

2 Answers 2

6

try this:

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[hash[0]] = hash[1];
        }
        return vars;
}

var url_vars = getUrlVars();
for(var i in url_vars)
{
        alert(i + " == " + url_vars[i]);
}   
Sign up to request clarification or add additional context in comments.

1 Comment

Wow why didn't I just do getUrlVars()...now I feel like an idiot :(
1

mdn

let url = new URL(a.href);
for (const [key, value] of url.searchParams) {

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.