Created
May 9, 2011 01:33
-
-
Save madrobby/961889 to your computer and use it in GitHub Desktop.
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
// 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); |
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)}
^^ Yeah, but that's gross, :P
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.