Javascript 页面根目录上location.hash的条件是什么?

Javascript 页面根目录上location.hash的条件是什么?,javascript,Javascript,我尝试了以下方法: console.log(location.hash) if(location.hash = ''){ console.log('home') } 例如,我应该设置什么条件才能让控制台登录到主页 example.com/您是否希望测试哈希是否存在 // If there is no hash if(!window.location.hash){ console.log("no hash"); } if(window.locat

我尝试了以下方法:

    console.log(location.hash)
    if(location.hash = ''){
        console.log('home')
    }
例如,我应该设置什么条件才能让控制台登录到主页
example.com/

您是否希望测试哈希是否存在

// If there is no hash
if(!window.location.hash){
    console.log("no hash");
}
if(window.location.pathname == '/'){
    console.log('home')
}

将不设置
位置散列

if (location.hash == null)
if (!location.hash)

在您的代码中,您正在分配!不要比较!使用
==

以下内容将登录example.com/

if(window.location.pathname == '/'){
    console.log('home')
}

您需要使用pathname属性,而不是hash

1等号是赋值而不是比较。我认为
location.hash
永远不会返回
null
false