Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Read URL GET variable
// Given a query string "?to=email&why=because&first=John&Last=smith"
// getUrlVar("to") will return "email"
// getUrlVar("last") will return "smith"
// Slightly more concise and improved version based on http://www.jquery4u.com/snippets/url-parameters-jquery/
function getUrlVar(key){
var result = new RegExp(key + "=([^&]*)", "i").exec(window.location.search);
return result && unescape(result[1]) || "";
}
// To convert it to a jQuery plug-in, you could try something like this:
(function($){
$.getUrlVar = function(key){
var result = new RegExp(key + "=([^&]*)", "i").exec(window.location.search);
return result && unescape(result[1]) || "";
};
})(jQuery);
@alkos333

This comment has been minimized.

Show comment
Hide comment
@alkos333

alkos333 Feb 8, 2012

If you would like to get all key/value pairs at once, use a variation of this function by Ashley Ford: http://papermashup.com/read-url-get-variables-withjavascript/

Owner

alkos333 commented Feb 8, 2012

If you would like to get all key/value pairs at once, use a variation of this function by Ashley Ford: http://papermashup.com/read-url-get-variables-withjavascript/

@loktar00

This comment has been minimized.

Show comment
Hide comment
@loktar00

loktar00 Oct 6, 2012

Nice elegant, thanks.

loktar00 commented Oct 6, 2012

Nice elegant, thanks.

@kevinfilteau

This comment has been minimized.

Show comment
Hide comment
@kevinfilteau

kevinfilteau Nov 12, 2012

I replaced unescape with decodeURIComponent to support utf-8 htmlentities characters.

I replaced unescape with decodeURIComponent to support utf-8 htmlentities characters.

@luckman212

This comment has been minimized.

Show comment
Hide comment
@luckman212

luckman212 Jan 28, 2013

Wonderful. Thank you!

Wonderful. Thank you!

@jv5000

This comment has been minimized.

Show comment
Hide comment
@jv5000

jv5000 Jan 16, 2015

I'm new to this so I apologize if this is obvious to some....

But how do I call getUrlVar(key) and use the result in another link
on my html page?

For example: http://FirstUrl.com/?id=123456

I need to extract the id (123456) from the First Url and use that
in my Second Url so the link is Clickable.

href=http://SecondUrl.com/?id=123456

Any help gratefully accepted, it's driving me nuts!!!

jv5000 commented Jan 16, 2015

I'm new to this so I apologize if this is obvious to some....

But how do I call getUrlVar(key) and use the result in another link
on my html page?

For example: http://FirstUrl.com/?id=123456

I need to extract the id (123456) from the First Url and use that
in my Second Url so the link is Clickable.

href=http://SecondUrl.com/?id=123456

Any help gratefully accepted, it's driving me nuts!!!

@jammin804

This comment has been minimized.

Show comment
Hide comment
@jammin804

jammin804 Mar 5, 2015

Thank you! THANK YOU! This helped me so much!

Thank you! THANK YOU! This helped me so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment