保留页面状态以使用浏览器后退按钮重新访问 [英] Preserve page state for revisiting using browser back button

查看:26
本文介绍了保留页面状态以使用浏览器后退按钮重新访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于用户按下按钮动态加载内容的页面:

I have a page that dynamically loads content based on a user pushing a button:

${document).ready(function)
{
    $("#myButton").click(function()
    {
        $("#dynamicDiv").load("www.example.com");
    });
}

动态内容工作正常,我可以整天获取页面.但是在您点击链接到另一个页面后,然后按浏览器后退按钮返回该页面,该页面将完全重置,就好像从未加载过任何动态内容一样.

The dynamic content works fine, I can fetch pages all day long. But after you follow a link to another page, then press the browser back button to come back to the page, the page is completely reset as though no dynamic content had ever been loaded.

我发誓我以前见过不同的行为,但也许我疯了.浏览器不应该保留页面的状态,而不是重新渲染它吗?

I swear I've seen different behavior before, but maybe I'm insane. Shouldn't the browser preserve the state of the page, rather than re-rendering it?

顺便说一下,我正在使用 Play!框架,如果这与此有关.

By the way, I'm using Play! framework, if that has any bearing on this.

推荐答案

浏览器加载第一次收到的页面.任何通过 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.

这篇关于保留页面状态以使用浏览器后退按钮重新访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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