Skip to content

Instantly share code, notes, and snippets.

@ugurhasar
Last active September 22, 2017 21:59
Show Gist options
  • Save ugurhasar/89720d40265f72f08b6a97073d53df0d to your computer and use it in GitHub Desktop.
Save ugurhasar/89720d40265f72f08b6a97073d53df0d to your computer and use it in GitHub Desktop.
JavaScript QueryString Fonksiyonu
function queryString()
{
var dictionary = [];
var queryStr = location.search.substring(1,location.search.length).replace(/(%20|\+)/g,' ');
var queries = queryStr.split('&');
for (i = 0; i < queries.length; i++) {
var key = '';
var value = '';
if(queries[i].indexOf('=') > -1)
{
key = queries[i].substring(0, queries[i].indexOf('='));
value = queries[i].substring(queries[i].indexOf('=') + 1, queries[i].length);
}
else
key = queries[i];
if(arguments.length > 0 && key == arguments[0])
return value;
if(!arguments[0]){
dictionary.push({
key: key,
value: value
});
}
}
return dictionary.length == 0 ? null : dictionary;
}
console.log(queryString('sample'));
console.log(queryString('index'));
console.log(queryString('id'));
console.log(queryString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment