Skip to content

Instantly share code, notes, and snippets.

@kadavre
Created June 18, 2017 15:05
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 kadavre/b61343478e58646859e3878a6f303a97 to your computer and use it in GitHub Desktop.
Save kadavre/b61343478e58646859e3878a6f303a97 to your computer and use it in GitHub Desktop.
var route = {
_routes : {}, // The routes will be stored here
add : function(url, action){
this._routes[url] = action;
},
run : function(){
jQuery.each(this._routes, function(pattern){
if(location.href.match(pattern)){
// "this" points to the function to be executed
this();
}
});
}
}
// Will execute only on this page:
route.add('002.html', function(){
alert('Hello there!');
});
route.add('products.html', function(){
alert("this won't be executed :(");
});
// You can even use regex-es:
route.add('.*.html', function(){
alert('This is using a regex!');
});
route.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment