I found a solution on this site: using-base-href-with-anchors that doesn’t require jQuery, and here is a working snippet:
<base href="https://example.com/">
<a href="http://stackoverflow.com/test">/test</a>
<a href="javascript:;" onclick="document.location.hash="test";">Anchor</a>
Or without inline JavaScript, something like this:
document.addEventListener('DOMContentLoaded', function(){
var es = document.getElementsByTagName('a')
for(var i=0; i<es.length; i++){
es[i].addEventListener('click', function(e) {
e.preventDefault()
document.location.hash = e.target.getAttribute('href')
})
}
})