如何使用 JavaScript 检查 URL 中的哈希值

根据当前 URL 的哈希 (#) 值修改网页元素。
我们可以通过运行 JavaScript 代码快速从 URL 获取哈希值。

我们可以使用 window.location.hash 方法来获取哈希:

let hash = window.location.hash;
console.log("window.location.hash : " + hash);
let hash = window.location.hash;
if (window.location.hash) {
  //Fragment exists
console.log("window.location.hash : " +  hash);
} else {
  //Fragment doesn't exist
 console.log("no  window.location.hash" );
}

hash 属性包含片段标识符,包括当前页面的散列“#”符号。

位置哈希属性返回片段标识符,包括当前页面的哈希“#”符号。
例如:

http://onitroad.com/#foo

其中位置哈希属性将返回#foo 的片段标识符。

window.location 属性

location 接口表示它所在对象的 URL。
对其所做的更改反映在与其相关的对象中。
window.location 是对 location 对象的引用,该对象表示该窗口中显示的文档的当前 URL。

日期:2020-06-02 22:16:09 来源:oir作者:oir