Chris Dutrow
Chris Dutrow

Reputation: 50362

Parse out a URL query argument in javascript

I'm using javascript and need to parse out a query argument that is a URL:

/folderone/two?link=http%3A%2F%2Fwww.cooldomainname.com

This is doubly hard because I not only need to parse out the query argument "link", but once I have that, the "://" seems to have been turned into: "%3A%2F%2F"

I got so far as to do this:

url.replace(/^.*\=/, '');

Which left me with:

http%3A%2F%2Fwww.cooldomainname.com

But now I still need to handle these "%3A%2F%2F" I could just do a find and replace, but I feel like there must be some type of library that I should be using to "de-URLify" query arguments?

Upvotes: 1

Views: 459

Answers (1)

Wayne
Wayne

Reputation: 60414

Use:

decodeURIComponent("http%3A%2F%2Fwww.cooldomainname.com");

Output:

"http://www.cooldomainname.com"

Note also that I think split is a more natural choice than replace:

"/folderone/two?link=http%3A%2F%2Fwww.cooldomainname.com".split("=")[1]

(It's assumed that your input string has just the one parameter.)

Upvotes: 1

Related Questions