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

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

问题描述

我想使用 window.location.hashhistory.pushState 更新 URL.

I want to update the URL using window.location.hash or history.pushState.

每种方法的区别和优点是什么?

What are the differences and advantages of each method?

推荐答案

location.hashhistory.pushState 方法.

pushState 方法的优点是可以将状态绑定到历史条目.

The advantage of the pushState method is that you can bind a state to the history entry.

如果你不需要这个状态对象,我推荐使用 location.hash 属性,以更好地兼容旧浏览器.

If you don't need this state object, I recommend to use the location.hash property, to have 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; 有什么区别?位置.hash?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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