Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zaus/5201739 to your computer and use it in GitHub Desktop.
Save zaus/5201739 to your computer and use it in GitHub Desktop.
Parse "hashbang" `#!` arguments like url query-string. [original source](https://gist.github.com/miohtama/1570295)
/**
* 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 url URL to parse or null if window.location is used
* @return Object of key -> value mappings.
* @source https://gist.github.com/zaus/5201739
*/
function hashbang(url, i, hash) {
url = url || window.location.href;
var vars = {}, hashes = url.slice(url.indexOf('#!') + 2).split('&');
for(i = hashes.length; i--;) {
hash = hashes[i].split('=');
vars[hash[0]] = hash.length > 1 ? hash[1] : null;
}
return vars;
}
@yarus23
Copy link

yarus23 commented Apr 13, 2013

this is a bit better:

    function hashbang(url, i, hash) {
        debugger;
        url = url || window.location.href;

        var pos = url.indexOf('#!');
        if( pos < 0 ) return [];
        var vars = [], hashes = url.slice(pos + 2).split('&');

        for(i = hashes.length; i--;) {
            hash = hashes[i].split('=');

            vars.push({ name: hash[0], value: hash.length > 1 ? hash[1] : null});
        }

        return vars;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment