简体   繁体   English

导航回具有相同状态的搜索页面 (JavaScript)

[英]Navigating back to Search Page with same state (JavaScript)

How do I, using JavaScript, retain the state of the search page, when a user clicks into a search result, but then goes back to the main search page.当用户点击搜索结果,然后返回主搜索页面时,我如何使用 JavaScript 保留搜索页面的状态。

eg例如

HTML : https://startech-enterprises.github.io/docs/guides/data-analytics/data-analytics.html HTML : https://startech-enterprises.github.io/docs/guides/data-analytics/data-analytics.html

There are 5 elements that determine what is shown on the page:有 5 个元素决定页面上显示的内容:

  • What tags are clicked in the three filter menus, on the side在侧面的三个过滤器菜单中单击了哪些标签
  • What search term is typed into the search menu bar, at the top在顶部的搜索菜单栏中输入了什么搜索词
  • The pagination page that is selected, at the bottom选中的分页页面,在底部

Problem is that whenever I go into the search result, and navigate back to the main search page, the search page resets itself, so I have to re-enter the search criteria again - which can be quite annoying.问题是,每当我进入搜索结果并导航回主搜索页面时,搜索页面都会自行重置,因此我必须再次重新输入搜索条件 - 这可能非常烦人。

If I click 'back' in the browser, the search page state is retained, but the search script stops working.如果我在浏览器中单击“返回”,则保留搜索页面状态,但搜索脚本停止工作。 Also, using the 'browser' back button only goes back one 'link' - so if the user clicks on several links in any page (returned from the search), they have to press the 'back' button many times, to get back to the main search page - which again isn't ideal.此外,使用“浏览器”后退按钮只能返回一个“链接”——因此,如果用户单击任何页面中的多个链接(从搜索返回),他们必须多次按下“后退”按钮才能返回到主搜索页面 - 这又不理想。

Any ideas on how to solve this?关于如何解决这个问题的任何想法? Would seem like a fairly common problem?似乎是一个相当普遍的问题?

The site is purely static, (generated using Markdown and Jekyll).该站点是纯静态的(使用 Markdown 和 Jekyll 生成)。 Site interactivity is set with Vanilla JavaScript, and SASS/SCSS.站点交互性是使用 Vanilla JavaScript 和 SASS/SCSS 设置的。

Pls do help!请帮忙!

Many thanks in advance.提前谢谢了。

Sachin萨钦

UPDATE: This has now been solved based on the answers given below更新:现在已经根据下面给出的答案解决了这个问题

You can simply do it by saving the data in users device .您可以通过将数据保存在用户设备中来简单地做到这一点。 You can do it by using cookies or by localStorage.您可以通过使用 cookie 或 localStorage 来实现。 I prefer localStorage because users can deny cookies easily.我更喜欢 localStorage,因为用户可以轻松拒绝 cookie。 Like this-像这样-

localStorage.setItem("tags",tagItemsInAnArray);

And later localStorage.getItem("tags");后来localStorage.getItem("tags");

You can use localStorage to keep the search filter data.您可以使用 localStorage 来保留搜索过滤器数据。 The concept is to keep all filter data in an array and keep that array in the localStorage of the browser before any redirection.这个概念是将所有过滤器数据保存在一个数组中,并在任何重定向之前将该数组保存在浏览器的 localStorage 中。 Here is an official documentation with implementation of localStorage. 是一个包含 localStorage 实现的官方文档。

Here is a demo:这是一个演示:

//before redirection
let filterData = [];
localStorage.setItem("searchFilter", JSON.stringify(filterData));

//after page-load
let cachedFilterData= JSON.parse(localStorage.getItem("searchFilter"));
if(cachedFilterData.length>0){
    //cache data exist
}

//when you need to delete cache
localStorage.removeItem("searchFilter");

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM