
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人
1.3.5 window.location.hash
window.location.hash对应RFC3986文档中的fragment(1.1节)。hash以“#”号开头,可通过设置window.location.hash改变URL的hash值。在通过window.location.hash设置hash值时,“#”号可省略,有时在书写时也可省略window.。

改变hash同样会产生新的历史栈记录:


在设置location.hash时要注意,如果设置的location.hash值与浏览器URL地址的hash值相同,就不会触发任何事件,也不会添加任何历史记录。或者如果前后两次对location.hash设置了相同的值,则仅第一次location.hash设置生效,第二次相同的设置不会产生任何事件和历史记录。
如果希望在改变地址栏hash的同时,也不进行入栈操作,则可通过window.location.replace实现,如:

类似于window.history.replaceState方法,其不会产生新的历史栈记录。