简体   繁体   English

我似乎无法弄清楚如何操纵浏览器历史记录

[英]I can't seem to figure out how to manipulate the browser history

So what I am trying to do is change data based on the navigation of a user.所以我想做的是根据用户的导航更改数据。 I have 6 main category pages.我有 6 个主要类别页面。 From those 6 main category pages, I have 12 sub categories that the user can select.从这 6 个主要类别页面中,我有 12 个子类别可供用户选择。

What I am trying to achieve:我正在努力实现的目标:

Let's say the user selects category one.假设用户选择类别一。

  • The user selects sub-category 1 from category 1.用户从类别 1 中选择子类别 1。
  • The sub-category page is shown and removes the data not related to category 1.显示子类别页面并删除与类别 1 无关的数据。
  • The user selects sub-category 2 from sub-category 1 and removes the data not related to category 1.用户从子类别 1 中选择子类别 2 并删除与类别 1 无关的数据。
  • The user selects sub-category 3 from sub-category 2 and removes the data not related to category 1.用户从子类别 2 中选择子类别 3,并删除与类别 1 无关的数据。

So I can do the category one to sub-category 1 but how would I do it for lets says sub-category 1 to 2 and from 2 to 3...n times....?因此,我可以将类别 1 设为子类别 1,但是我将如何执行子类别 1 到 2 以及从 2 到 3 ...... n 次......? Each sub-category has to know which main category was last selected.每个子类别都必须知道最后选择的是哪个主要类别。

I have looked at but can't seem to find a solution.我已经看过了,但似乎找不到解决方案。

window.location.hash
history.pushState("","","");
document.referrer

Typically this is a bad design and I understand that but I am limited to what I can do on the server side.通常这是一个糟糕的设计,我理解这一点,但我只能在服务器端做些什么。 So trying to develop a work around....所以试图开发一个解决方法......

After trying to work with these and not getting the desired behavior, I was able to find the best solution through several hours of research.在尝试使用这些并没有获得所需的行为后,我能够通过几个小时的研究找到最佳解决方案。 The only alternative I could find for passing around data on the client side between pages.我能找到的唯一替代方法是在页面之间的客户端传递数据。

window.location.hash
history.pushState("","","");
document.referrer

So lets say the user navigates to category1假设用户导航到 category1

You would set a session storage object您将设置会话存储对象

sessionStorage.setItem('last-category', "category1");

The user navigates sub-category 1. You can use this to get the session storage of an object and will show the last set operation for that object.用户导航子类别 1。您可以使用它来获取对象的会话存储并显示该对象的最后一次设置操作。

sessionStorage.getItem("last-category"); -> output category1

The user navigates sub-category 2用户导航子类别 2

sessionStorage.getItem("last-category"); -> output category1

The user navigates to category 2 You would set the session storage object again to show category2用户导航到类别 2 您将再次设置会话存储对象以显示类别 2

sessionStorage.setItem('last-category', "category2");

The user navigates to sub-category 1用户导航到子类别 1

sessionStorage.getItem("last-category"); -> output category2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我似乎无法弄清楚正则表达式 - I can't seem to figure out the regular expression 我的CSS问题似乎无法弄清楚 - Issue with my CSS that I can't seem to figure out 我似乎无法弄清楚如何向指定用户发送消息 - I can't seem to figure out how to send a message to a specified user 我似乎无法弄清楚如何进行字符串插值; 代码排练习 - I can't seem to figure out how to do a string interpolation; Code Platoon Exercise 无法弄清History JS - Can't figure out History JS 我似乎无法弄清楚如何修复JavaScript中的两个棉绒。 有人可以帮我理解吗? - I can't seem to figure out how to fix two lints in my javascript. Can someone help me understand? 似乎无法弄清楚如何 get.toggle function 在 jQuery 中工作,包括该库,我有一个用于测试的 jsFiddle - Can't seem to figure out how to get .toggle function working in jQuery, the library is included, I have a jsFiddle for testing 无法弄清楚如何使用JavaScript在浏览器中导入模块 - Can't figure out how to import modules in browser with javascript 我的javascript函数似乎无法正常工作。 我不知道为什么 - My javascript function doesn't seem to be working. I can't figure out why 我做了一个无限循环,似乎无法弄清楚我做错了什么 - I've made an infinite loop and can't seem to figure out what I did wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM