当 URL 的哈希值或加载后哈希值发生变化时执行代码?

Execute Code When The URL Has Hash OR HASH Changes After Load?

如有任何帮助,我们将不胜感激!

加载时存在哈希时执行代码:

if(location.hash) { //code; }

哈希值变化时执行代码:

$(window).hashchange(function(){//code;});

如何合并这两项检查?
这不起作用:

if(location.hash || $(window).hashchange(function() { //code; });

theCode 定义一个函数,将其引用传递给 hashchange 事件并在页面加载时检查哈希是否存在直接执行它

function theCode() { //code; }

if(location.hash){
    theCode();
}

$(window).hashchange(theCode);