Skip to content

Instantly share code, notes, and snippets.

@chrisevans
Forked from jed/LICENSE.txt
Created August 6, 2011 06:56
Show Gist options
  • Save chrisevans/1129100 to your computer and use it in GitHub Desktop.
Save chrisevans/1129100 to your computer and use it in GitHub Desktop.
route client urls with 404s and pattern captures
function(
a, // an array: even indices are patterns, odd indices as callbacks
b, // the interval at which to poll for hash changes
c // placeholder for previous hash
){
function e( // polling function
d // placeholder for current hash
){
d = location.hash; // set the current hash.
if (d != c) { // if the current and previous hashes differ,
for ( // loop first by
b = 0, // initializing the cursor and
c = d; // overwriting the previous hash with the current one, and then
(d = a[b++]) // while there is still a pattern to be matched,
&&!(d = d.exec(c)); // stop only when the pattern matches the hash.
b++ // increment the cursor. it increases by 2 each iteration.
); // if no patterns were matched, the last one (404) is used.
a[b](d) // call the callback associated with the matched pattern
}
};
e(); // auto-run it on load, and
setInterval( // keep executing
e, // the polling function
b // at the specified interval,
|| 99 // or every 99ms.
)
}
function(a,b,c){function e(d){d=location.hash;if(d!=c){for(b=0,c=d;(d=a[b++])&&!(d=d.exec(c));b++);a[b](d)}};e();setInterval(e,b||99)}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Jed Schmidt <http://jed.is>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "route",
"keywords": ["router", "URL", "hash", "location", "controller"]
}
<!-- see this page here: https://s3.amazonaws.com/paulirishfanclub/index.html -->
<script>
// Define the route function
var route = function(a,b,c){function e(d){d=location.hash;if(d!=c){for(b=0,c=d;(d=a[b++])&&!(d=d.exec(c));b++);a[b](d)}};e();setInterval(e,b||99)}
// Define the renderer
function render(body) {
document.open();
document.write(
"<body style='font-size:300%;color:fuchsia;background-color:papayawhip'>" +
header +
body +
footer +
"</body>"
);
document.close()
}
// and some html
var nav = "<a href='#pics'>PICS</a> | <a href='#vids'>VIDS</a> | <a href='#home'>HOME</a>"
var header = "<header>Welcome to my Paul Irish Fanpage!</header><div>[ " + nav + " ]</div>"
var footer = "<footer>This is the footer. Make sure you check the source!</footer>"
// Route up our app!
route([
/^$/ , function(){ location.hash = "home" },
/^#home$/ , function(){ render( "Click the links above. <a href='#paul_irish'>This link</a> doesn't work." ) },
/^#pics$/ , function(){ render( '<img src="http://farm3.static.flickr.com/2557/3919895524_0386a7de8c_z.jpg">' ) },
/^#vids$/ , function(){ render( '<iframe src="http://www.youtube.com/embed/N8SS-rUEZPg" width="560" height="349"></iframe>') },
/* 404 */ , function(){ render( "404: Not found." ) }
])
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment