Forked from miohtama/parse-hash-bang-arguments-in-javascript.js
Last active
August 29, 2015 14:22
-
-
Save tagr/20fc1f0d6996a27a3d7a to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* 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