Forked from miohtama/parse-hash-bang-arguments-in-javascript.js
Created
October 6, 2016 17:43
-
-
Save cagartner/fcef57719d07f612af8b2ade8d4b0f2f to your computer and use it in GitHub Desktop.
Parse hash bang HTTP GET query style arguments from an URL using Javascript
This file contains hidden or 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 aURL URL to parse or null if window.location is used | |
* | |
* @return Object of key -> value mappings. | |
*/ | |
function parseHashBangArgs(aURL) { | |
aURL = aURL || window.location.href; | |
var vars = {}; | |
var hashes = aURL.slice(aURL.indexOf('#') + 1).split('&'); | |
for(var i = 0; i < hashes.length; i++) { | |
var hash = hashes[i].split('='); | |
if(hash.length > 1) { | |
vars[hash[0]] = hash[1]; | |
} else { | |
vars[hash[0]] = null; | |
} | |
} | |
return vars; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment