jQuery Hashchange Event Plugin

Posted by Bryan in Web Development

Looking for a Javascript plugin to react to the hash value changing on your page?

The jQuery Hashchange Plugin will enable to you attach to the hashchange event for all clients.

<script type="text/javascript">
// the callback will receive the new and old hash values as arguments
$(window).hashChange(callbackFunction);

// or you can bind to the "hashChange" event
$(window).bind("hashChange", function(e, newHash, oldHash) {
    console.log("new: " +  newHash);
    console.log("old: " + oldHash);
});
</script>

Basically, everytime the hash value changes, you can respond as needed by the requirements of your page. This is useful for ajax views, tabs, and other layout changes in your page that need to be based on the hash state.

The plugin also supports Modernizr v2.0+ to detect the "hashchange" event for modern browsers.

The jquery.hashable.js the source code.

View an example HTML page.