I found the problem, it conflicted with a
history.pushState function and I was able to fix it by simply adding
+location.hash to it. Though this seems kind of hacky to me since this function is only executed after the document is loaded and does not alter the HTML of the page. Nevertheless thank you for your reply!