Skip to content

Instantly share code, notes, and snippets.

@alexbaulch
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexbaulch/283d2b493ce09416c2a0 to your computer and use it in GitHub Desktop.
Save alexbaulch/283d2b493ce09416c2a0 to your computer and use it in GitHub Desktop.
A small CommonJS module for fetching the value of a given url param.
module.exports = function(variable) {
//get query params and remove the beginning '?'
var query = window.location.search.substring(1);
//split on ampersand
var vars = query.split('&');
//loop through 'vars' array
for (var i = 0; i < vars.length; i++) {
// split on '=' to give key value pairs
var pair = vars[i].split('=');
// if key matchs key supplied to function
if (pair[0] == variable) {
// return the value of the param that has matched
return pair[1];
}
}
return false;
};
// require the module
var getQueryVariable = require('./get-query-variable');
// fetch query variable
var page = getQueryVariable('page');
// live the dream
alert(page);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment