Created
May 30, 2018 16:21
-
-
Save arifhp86/174e98c7b8a72956a2e12936708a6f05 to your computer and use it in GitHub Desktop.
GET Superglobal in Javascript
This file contains hidden or 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
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