Tuesday, July 31, 2012

Needed Features in HTML5 history.state

Though history.pushState is a much better implementation than hash-tags to mimic the behavior of an HTTP request, it still has some ways to go.


A URL Change Event


When a hashfragement is changed, only javascript and scrolling is triggered. This needs to be extended to all of a URL to be supported. The best implementation is to trigger the popstate event which can be called with the code:
window.addEventListener('popstate', function(event) {
            // Code goes here
}

If the popstate event listener does NOT contain an anonymous function or the anonymous function does not contain:
event.preventDefault();
or whatever you named the event object, then the HTTP-GET request should be triggered as usual.

REST Support

It is a shame that what with the web's frameworks now implementing MVC or MVVM that history.pushState lacks REST support. It should pursue a more simplistic approach since javascript obviously isn't REST, but still same idea. Routes should be defined as:
history.state.addRoute(name, urlMap).

urlMap should be a string in the format:
history.state.addRoute("TestRoute", "/conditionalSegment1/cd2/{history.state.variable1}/{history.state.variable2}/{history.state.variable3}")

That would make HTML5 soooooo attractive.

Read more awesomeness from Ari Falkner >