Skip to content

Instantly share code, notes, and snippets.

@sunny
Created August 13, 2008 11:50
Show Gist options
  • Save sunny/5231 to your computer and use it in GitHub Desktop.
Save sunny/5231 to your computer and use it in GitHub Desktop.
/*
* hash_change_listener(hash_check_interval)
*
* Prototype method for launching a "hash:change" custom event when the
* page's #anchor changes.
*
* Examples: adding a class to the current page anchor.
*
* document.observe('hash:change', function(event) {
* var div = $(window.location.hash.replace(/^#/, ''))
* if (div) {
* $$('.current_anchor').invoke('removeClassName', 'current_anchor')
* div.addClassName('current_anchor')
* }
* })
*
* hash_change_listener(300)
*/
function hash_change_listener(hash_check_interval) {
var hash_current = null
var fire_on_change = function() {
if (window.location.hash != hash_current) {
document.fire('hash:change')
hash_current = window.location.hash
}
}
fire_on_change()
return setInterval(fire_on_change, hash_check_interval)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment