Last active
August 29, 2015 14:07
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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