且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

保留页面状态以使用浏览器后退按钮重新访问

更新时间:2023-08-31 22:46:10

浏览器加载第一次收到的页面.任何通过 javascript 完成的 DOM 修改都不会被保留.

The browser loads the page as it was first received. Any DOM modifications done via javascript will not be preserved.

如果您想保留修改,您将不得不做一些额外的工作.修改 DOM 后,使用标识符更新 url 哈希,您可以稍后解析并重新创建修改.无论何时加载页面,您都需要检查是否存在哈希值并根据标识符进行 DOM 修改.

If you want to preserve the modifications you will have to do some extra work. After you modify the DOM, update the url hash with an identifier that you can later parse and recreate the modification. Whenever the page is loaded you need to check for the presence of a hash and do the DOM modifications based on the identifier.

例如,如果您要动态显示用户信息.每次显示一个时,您都会将 url 哈希更改为如下所示:#/user/john".每当页面加载时,您都需要检查哈希是否存在 (window.location.hash),解析它并加载用户信息.

For example if you are displaying user information dynamically. Every time you display one you would change the url hash to something like this: "#/user/john". Whenever the page loads you need to check if the hash exists (window.location.hash), parse it, and load the user information.