Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tagr/20fc1f0d6996a27a3d7a to your computer and use it in GitHub Desktop.
Save tagr/20fc1f0d6996a27a3d7a to your computer and use it in GitHub Desktop.
/**
* Parse hash bang parameters from a URL as key value object.
*
* For repeated parameters the last parameter is effective.
*
* If = syntax is not used the value is set to null.
*
* #!x+y:3 -> { x:null, y:3 }
*
* @param options
*
* @return Object of key -> value mappings.
*/
function parseHashBangArgs(/*Object*/options) {
options = options || {};
var href = options.href || window.location.href;
if (!!!(href.indexOf("#!") + 1)) return false;
var vars = {},
paramDelimiter = options.paramDelimiter || "+",
valueDelimiter = options.valueDelimiter || ":",
hashes = href.slice(href.indexOf("#!") + 2).split(paramDelimiter),
i = hashes.length;
while (i--) {
var hash = hashes[i].split(valueDelimiter);
vars[hash[0]] = (hash.length > 1)
? hash[1]
: null;
}
return vars;
};
console.log(parseHashBangArgs());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment