How to get "GET" variables from request in JavaScript?
Does jQuery ou YUI! has this feature built-in?
How to get "GET" variables from request in JavaScript?
Does jQuery ou YUI! has this feature built-in?
You can parse the URL of the current page to obtain the GET parameters. The URL can be found by using location.href
.
You can use the URL to acquire the GET variables. In particular, window.location.search
gives everything after (and including) the '?'. You can read more about window.location here.
All data is available under
window.location.search
you have to parse the string, eg.
function get(name){
if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))
return decodeURIComponent(name[1]);
}
just call the function with GET variable name as parameter, eg.
get('foo');
this function will return the variables value or undefined if variable has no value or doesn't exist
If you already use jquery there is a jquery plugin that handles this: