Вы можете использовать событие hashchange .
function hashchanged(){
var hash = location.hash.replace( /^#/, '' );
//your code
}
window.addEventListener("hashchange", hashchanged, false);
или встроенный плагин jquery hashchange
$(function(){
// Bind the event.
$(window).hashchange(hashchanged);
// Trigger the event (useful on page load).
hashchanged();
});
function hashchanged(){
var hash = location.hash.replace( /^#/, '' );
//your code
}