history.pushState&之间有什么区别? location.hash? [英] What are the differences between history.pushState & location.hash?

查看:197
本文介绍了history.pushState&之间有什么区别? location.hash?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用其中任何一个更新URL

I'm trying to update the URL using either

  • window.location.hash
  • history.pushState.
  • window.location.hash or
  • history.pushState.

两者的区别/优点是什么?

What are the differences / advantages of each?

推荐答案

location.hash 方法.
pushState方法的优点是您可以将状态绑定到历史记录条目.
如果不需要此状态对象,建议使用location.hash属性,以便与旧版浏览器更好地兼容.

location.hash has a better support than the history.pushState method.
The advantage of the pushState method is that you can bind a state to the history entry.
If you don't need this state object, I recommend to use the location.hash property, to have a better compatibility with older browsers.

location.hash = 'new-hash';
console.log(history.state); // null or undefined

history.pushState({extraData: "some state info"}, '', 'new-hash'); //<---
console.log(history.state); // [object Object] = {"extraData": "some state info"}

这篇关于history.pushState&amp;之间有什么区别? location.hash?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆