Skip to content

Instantly share code, notes, and snippets.

@arifhp86
Created May 30, 2018 16:21
Show Gist options
  • Save arifhp86/174e98c7b8a72956a2e12936708a6f05 to your computer and use it in GitHub Desktop.
Save arifhp86/174e98c7b8a72956a2e12936708a6f05 to your computer and use it in GitHub Desktop.
GET Superglobal in Javascript
var getParam = (function() {
var query = location.search.substr(1);
var params = {};
if(query) {
query.split("&").forEach(function(part) {
var item = part.split("=");
params[item[0]] = decodeURIComponent(item[1]);
});
}
return function(paramName) {
if(undefined != paramName) {
return typeof params[paramName] !== 'undefined' ? params[paramName] : '';
} else {
return params;
}
}
})();
// https://testdomain.com/page1/?utm_source=yahoo.com&utm_medium=b1_publisher
console.log(getParam()); //{utm_source: "yahoo.com", utm_medium: "b1_publisher"}
console.log(getParam('utm_source')); // "yahoo.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment