Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
get query variable

use like this:

getQueryVariable("foo")

To get variables in urls eg:

http://foo.com/index.html?foo=bar

If you want to support arrays, check the version by @atk below:

function(a){
return RegExp("[&?]"+a+"=([^&]+)").exec(location)[1]
}
//searches the url (location)
//finds the text after a "&" or a "?" followed by the variable name, followed by a "="
//and before another "&"
function(a){return RegExp("[&?]"+a+"=([^&]+)").exec(location)[1]}
//If you want to support arrays (Thanks atk!)
function(a){for(var b=[],c,d=eval('/[&?]'+a+'(\\[])?=([^&]+)/g');c=d.exec(location);)b.push(c[2]);return b}
{
"name": "getQueryVariable",
"description": "Get variables in urls",
"keywords": [
"url",
"query",
"string",
"variable",
"get"
]
}
<script>
getQueryVariable = function(a){return RegExp("[&?]"+a+"=([^&]+)").exec(location)[1]}
document.write(getQueryVariable("foo"))
</script>
@atk

This comment has been minimized.

Show comment Hide comment
@atk

atk May 15, 2012

&|\\? will not work properly, as it will match either "&" or "?[identifier=]", therefore returning the whole [identifier]=[value] string if it is preceded by "&". A working version would be:

function(a){return(RegExp('[&?]'+a+'=([^&;]+)').exec(location)||0)[1]}

atk commented May 15, 2012

&|\\? will not work properly, as it will match either "&" or "?[identifier=]", therefore returning the whole [identifier]=[value] string if it is preceded by "&". A working version would be:

function(a){return(RegExp('[&?]'+a+'=([^&;]+)').exec(location)||0)[1]}

@williammalo

This comment has been minimized.

Show comment Hide comment
@williammalo

williammalo May 15, 2012

@atk
Wow! I made a stupid mistake there =P
That code is very smart!
I didn't know that exec can reference stuff in parentheses! What a noob I am!

Owner

williammalo commented May 15, 2012

@atk
Wow! I made a stupid mistake there =P
That code is very smart!
I didn't know that exec can reference stuff in parentheses! What a noob I am!

@atk

This comment has been minimized.

Show comment Hide comment
@atk

atk May 15, 2012

@williammalo: we live and learn :-)

atk commented May 15, 2012

@williammalo: we live and learn :-)

@tsaniel

This comment has been minimized.

Show comment Hide comment
@tsaniel

tsaniel May 16, 2012

What about using split with regex?

tsaniel commented May 16, 2012

What about using split with regex?

@atk

This comment has been minimized.

Show comment Hide comment
@atk

atk May 16, 2012

The original code did, but exec is much smaller.

atk commented May 16, 2012

The original code did, but exec is much smaller.

@FarSeeing

This comment has been minimized.

Show comment Hide comment
@FarSeeing

FarSeeing May 17, 2012

What about supporting array values (e.g. x[]=1&x[]=2)?

What about supporting array values (e.g. x[]=1&x[]=2)?

@williammalo

This comment has been minimized.

Show comment Hide comment
@williammalo

williammalo May 18, 2012

@FarSeeing
I'm sorry but I have no idea what you mean by that.

Owner

williammalo commented May 18, 2012

@FarSeeing
I'm sorry but I have no idea what you mean by that.

@FarSeeing

This comment has been minimized.

Show comment Hide comment
@FarSeeing

FarSeeing May 21, 2012

@williammalo
The current version throws an error, if you try to search for array parameters:

// location = 'http://example.com?a=1&b[]=2&b[]=3'
getQueryVariable = function(a){return RegExp("[&?]"+a+"=([^&]+)").exec(location)[1]}
getQueryVariable( 'a' ); // "1", correct
getQueryVariable( 'b' ); // TypeError: RegExp("[&?]" + a + "=([^&]+)").exec(txt) is null
getQueryVariable( 'b[]' ); // TypeError: RegExp("[&?]" + a + "=([^&]+)").exec(txt) is null

@williammalo
The current version throws an error, if you try to search for array parameters:

// location = 'http://example.com?a=1&b[]=2&b[]=3'
getQueryVariable = function(a){return RegExp("[&?]"+a+"=([^&]+)").exec(location)[1]}
getQueryVariable( 'a' ); // "1", correct
getQueryVariable( 'b' ); // TypeError: RegExp("[&?]" + a + "=([^&]+)").exec(txt) is null
getQueryVariable( 'b[]' ); // TypeError: RegExp("[&?]" + a + "=([^&]+)").exec(txt) is null
@tsaniel

This comment has been minimized.

Show comment Hide comment
@tsaniel

tsaniel May 21, 2012

A version which supports array:
function(a,b,c){with(eval('/[&?]'+a+'(\\[])?=([^&]+)/g'))for(b=[];c=exec(location);)b.push(c[2]);return b}

tsaniel commented May 21, 2012

A version which supports array:
function(a,b,c){with(eval('/[&?]'+a+'(\\[])?=([^&]+)/g'))for(b=[];c=exec(location);)b.push(c[2]);return b}

@atk

This comment has been minimized.

Show comment Hide comment
@atk

atk May 21, 2012

Those TypeErrors can be caught with a (...||0)[1] construct as in my proposal.

atk commented May 21, 2012

Those TypeErrors can be caught with a (...||0)[1] construct as in my proposal.

@williammalo

This comment has been minimized.

Show comment Hide comment
@williammalo

williammalo May 21, 2012

@tsaniel
If you can make a version without "with" I'll update my function. (I have withophobia)

Owner

williammalo commented May 21, 2012

@tsaniel
If you can make a version without "with" I'll update my function. (I have withophobia)

@atk

This comment has been minimized.

Show comment Hide comment
@atk

atk May 22, 2012

Only 1 char longer:
function(a){for(var b=[],c,d=eval('/[&?]'+a+'(\\[])?=([^&]+)/g');c=d.exec(location);)b.push(c[2]);return b}

atk commented May 22, 2012

Only 1 char longer:
function(a){for(var b=[],c,d=eval('/[&?]'+a+'(\\[])?=([^&]+)/g');c=d.exec(location);)b.push(c[2]);return b}

@williammalo

This comment has been minimized.

Show comment Hide comment
@williammalo

williammalo May 22, 2012

@atk
Great! it works perfectly! (I think...)
I'll put it in my gist!
Thanks!

Owner

williammalo commented May 22, 2012

@atk
Great! it works perfectly! (I think...)
I'll put it in my gist!
Thanks!

@onhate

This comment has been minimized.

Show comment Hide comment
@onhate

onhate Nov 20, 2013

If you have an anchor on url like param=value#anchor it returns the value like value#anchor, you just need to add one more byte to the code to works =D

function url(a){for(var b=[],c,d=eval('/[&?]'+a+'([])?=([^&^#]+)/g');c=d.exec(location);)b.push(c[2]);return b}

onhate commented Nov 20, 2013

If you have an anchor on url like param=value#anchor it returns the value like value#anchor, you just need to add one more byte to the code to works =D

function url(a){for(var b=[],c,d=eval('/[&?]'+a+'([])?=([^&^#]+)/g');c=d.exec(location);)b.push(c[2]);return b}

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