WebDevelopment, ColdFusion, Railo, JS, Database and Tech-related by the Co-Founder and CEO of tunesBag.com

Friday 3 September 2010

Native hashchange event - Good bye polling

I just discovered that there is a native HashChange event in the latest version of several browsers (FF, Webkit, IE) - this way navigation solutions working with the hash at the end of a URL work without nasty timers.


To add the event, simply use:


if ( window.addEventListener ) {

window.addEventListener( 'hashchange', function () { checkHashChange(location.hash) }, false );

} else if ( window.attachEvent ) {

window.attachEvent( 'onhashchange' , function () { checkHashChange(location.hash) } );

}


Take a look at these samples as well. In real world applications you still will need to use a combination of both methods (polling + events).


Concerning the support of the feature in various browsers, please click here for a comparison chart.