Skip to content

@alkos333 /gist:1771618

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
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
function getUrlVar(key){
var result = new RegExp(key + "=([^&]*)", "i").exec(;
return result && unescape(result[1]) || "";
// To convert it to a jQuery plug-in, you could try something like this:
$.getUrlVar = function(key){
var result = new RegExp(key + "=([^&]*)", "i").exec(;
return result && unescape(result[1]) || "";

If you would like to get all key/value pairs at once, use a variation of this function by Ashley Ford:


Nice elegant, thanks.


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


Wonderful. Thank you!


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:

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


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


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
Something went wrong with that request. Please try again.