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:
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
Great! it works perfectly! (I think...)
I'll put it in my gist!
Thanks!
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}
Only 1 char longer:
function(a){for(var b=[],c,d=eval('/[&?]'+a+'(\\[])?=([^&]+)/g');c=d.exec(location);)b.push(c[2]);return b}