Skip to content

Instantly share code, notes, and snippets.

@tsq
Created December 27, 2015 12:47
Show Gist options
  • Save tsq/7a6a54de6304fbffa44b to your computer and use it in GitHub Desktop.
Save tsq/7a6a54de6304fbffa44b to your computer and use it in GitHub Desktop.
get qs args
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
// query string arguments
function getQueryStringArgs() {
// get query string without the initial?
var qs = (location.search.length > 0 ? location.search.substring(1) : '');
// obj to hold data
var args = {};
// get individual items
var items = qs.length ? qs.split('&') : [];
var item = null;
var name = null;
var value = null;
var i = 0;
var len = items.length;
// assign each item onto the args obj
for (i = 0; i < len; i++) {
item = items[i].split('=');
name = decodeURIComponent(item[0]);
value = decodeURIComponent(item[1]);
if (name.length) {
args[name] = value;
}
}
return args;
}
var args = getQueryStringArgs();
console.log(args);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment