javascript on url anchor change event

JavaScript
window.addEventListener('hashchange', function() {
...
})

Source

Also in JavaScript: