JavaScript function to parse the query string part of a URL.
/**
* Parses the query string part of a URL.
*
* This method returns a simple key/value map. If there are duplicate keys the
* latest value is returned.
*
* @param {string} queryString
* @return {!Object<string, string>}
*/
function parseQueryString(queryString) {
var params = {};
if (!queryString) {
return params;
}
if (queryString.startsWith('?') || queryString.startsWith('#')) {
queryString = queryString.substr(1);
}
var pairs = queryString.split('&');
for (var i = 0; i < pairs.length; i += 1) {
var pair = pairs[i];
var eqIndex = pair.indexOf('=');
var _name;
var value;
if (eqIndex != -1) {
_name = decodeURIComponent(pair.substring(0, eqIndex)).trim();
value = decodeURIComponent(pair.substring(eqIndex + 1)).trim();
}
else {
_name = decodeURIComponent(pair).trim();
value = '';
}
if (_name) {
params[_name] = value;
}
}
return params;
}