Created
June 18, 2017 15:05
-
-
Save kadavre/b61343478e58646859e3878a6f303a97 to your computer and use it in GitHub Desktop.
This file contains 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
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