19Jul/112
PathJS and HTML5 History – A Match Made in Heaven
Nobody will argue that the HTML5 History API was a step in the right direction in regards to Web Applications. Unfortunately, there was no simple, consolidated way that provided a routing mechanic that was both versatile and easy to manage. As of today, the simple routing mechanic of PathJS is fully compatible with the HTML5 History API. You get the power of a manual, with the simplicity of an automatic. Here's how it works:
// Define all your routes as you usually would with PathJS - Simply omit the "#"
Path.map("/home").to(function(){
console.log("Welcome home!");
});
Path.map("/users").to(function(){
console.log("Users, users, users.");
});
Path.map("/contact").to(function(){
console.log("Feel free to contact me via Github.");
});
// Now override the click events for any of your links, and call
// the Path.history.pushState method to invoke the PathJS router.
// This example uses jQuery.
$("a").live("click", function(event){
event.preventDefault();
Path.history.pushState({}, "", $(this).attr("href"));
});
It's simple as that. The PathJS implementation of the HTML5 History API supports the same features as regular PathJS, including: Before-filter chaining, enter/exit methods, parameterized routes, and optional route parameters. You can read more about it's features and limitations on the Github Page.
July 24th, 2011 - 22:48
Will path fallback to hashbang if html5 isn’t supported?
July 24th, 2011 - 23:14
By default, it will not fall back to hashtags if HTML5 isn’t supported. This is because some users of the library don’t want that functionality. Many users may want to use PathJS and the HTML5 API for simply client0side routing that adds nice transitions to their site, but won’t break for users without JavaScript. If you want PathJS to fall back to hashtags if HTML5 isn’t supported, you need to write two sets of routes, one with a hashtag, and one without. Each set of routes should be defined within a method. Upon page load, simply check if the HTML5 History API is supported, and call the appropriate method to define your desired routes. I’ll look into adding this as a feature in a future update.