Skip to content

Instantly share code, notes, and snippets.

@madrobby
Created May 9, 2011 01:33
Show Gist options
  • Save madrobby/961889 to your computer and use it in GitHub Desktop.
Save madrobby/961889 to your computer and use it in GitHub Desktop.
// routes = [ [/test/, function(url){ console.log(url) }], ... ]
setInterval(function h(r){if(h.l!==(h.l=location.hash))for(r in routes)routes[r][0].test(h.l)&&routes[r][1](h.l)},10);
@danilocelic
Copy link

setInterval(function h(){r=routes;if(h.l!=(l=h.l=location.hash))for(a in r)r[a][0].test(l)&&r[a]1},10);

@danbeam
Copy link

danbeam commented May 9, 2011

Depending on your browser or environment you can also use window.onhashchange (if this is for Zepto and you're only supporting Webkit and possibly other "modern" browser in the future, it'd SO work). However, it's 4 bytes more than danilocelic's answer.

addEventListener('hashchange',function(h,k,r){h=location.hash;r=routes;for(k in r)r[k][0].test(h)&&r[k][1](h)});

@robotlolita
Copy link

Well, if you don't care about overwriting events, you could drop the addEventListener entirely and, as long as the code runs in the DOMWindow context, bind directly to onhashchange. You could also save 5 more bytes by letting h, k and r leak into the global scope.

Also, the semicolon is pretty pointless...

this.onhashchange=function(h,k,r){h=location.hash;r=routes;for(k in r)r[k][0].test(h)&&r[k][1](h)}

@danbeam
Copy link

danbeam commented May 9, 2011

^^ Yeah, but that's gross, :P

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