Skip to content

Instantly share code, notes, and snippets.

@andremedeiros
Forked from jed/LICENSE.txt
Created May 17, 2011 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andremedeiros/976278 to your computer and use it in GitHub Desktop.
Save andremedeiros/976278 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)}
Copyright (c) 2011 Jed Schmidt, http://jed.is
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{
"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