javascript - 更改 window.location.href 后无法返回
问题描述
在我的脚本中,我正在跟踪我在网页中使用的标签
window.location.href = #!hashName1
如果我然后单击另一个选项卡,它将转到 #!hashName2
我的问题是,如果我单击后退按钮,它只会回到状态 #!hashName1。然后我必须再次单击返回以返回另一个页面。
有没有办法让它回到一个页面而不是回到以前的哈希状态?
谢谢
解决方案
你可以像这样处理后退按钮事件
window.onhashchange = function() {
goBack();
}
function goBack() {
window.location.hash = window.location.lasthash[window.location.lasthash.length-1];
//blah blah blah
window.location.lasthash.pop();
}
推荐阅读
- node.js - 从服务器接收信息后运行应用程序
- java - 在 Android Studio 中更新 GridView 颜色而不刷新
- c# - 在 postgresql 的 jsonb[] 类型列中插入数据的正确语法是什么
- azure - Azure Devops - 历史代理池数据(过去 30 天)
- javascript - repl 进程意外死亡-Segmentation-fault core dumped
- c - 我需要帮助在字符串中搜索子字符串,同时避免包含子字符串的单词
- reactjs - Safari 为元素 Nextjs、React、Spring 添加了模糊轨迹
- c++ - 无法打开源文件“locale.h”(“iostream”的依赖项)
- php - Laravel 应用程序有时会显示“Safari 无法打开页面”错误,在所有其他浏览器中都可以正常工作
- sql-server - SQL server ORDER BY - 列包含在索引中时速度快,不包含时超慢