改变url,但不重新向服务器请求资源

114 阅读1分钟

改变url的hash

  • URL的hash也就是锚点(#),本质上是改变window.location的href属性
  • 也可直接赋值location.hash来改变href,但是页面不发生刷新

location.hash = 'about'

HTML5的history模式:pushState

history.pushState({}, '', 'home')

HTML5的history模式:replaceState

history.replaceState({}, '', 'about')

HTML5的history模式:go

history.back() 等价于 history.go(-1)

history.forward() 等价于 history.go(1)