在后退按钮上保留动态更改的 HTML [英] Preserve dynamically changed HTML on back button

查看:25
本文介绍了在后退按钮上保留动态更改的 HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

太棒了,我经常看到它在其他网站上运行,但从未在我正在开发的网站上运行.

It's amazing, I constantly see this working in other sites but never in sites that I'm working on.

我正在使用 ajax 引入新内容,我了解 history.js 和 History API,我不想更改 URL,只需让浏览器缓存新的 HTML 内容,以便当用户离开页面时使用后退按钮返回,它仍然具有更新的 HTML.

I'm bringing in new content with ajax, I know about history.js and the History API, I do not want to change the URL, just have the browser cache the new HTML content so when a user leaves the page and comes back using the back button, it still has the updated HTML.

我看到这在其他网站上一直有效,无需更改 URL 或使用哈希 #.
有什么技巧可以让它工作还是由浏览器随机决定?
如果我不想使用 URL 来获取此信息,是否有其他简单的替代方法?

I see this working all the time in other sites without URL changes or using the hash #.
Is there a trick to get it to work or is it randomly decided by the browser?
If I don't want to use the URL to have this information, is there an easy alternative?

推荐答案

大约 15 年以来,我一直在使用两个我曾经通过痛苦的反复试验发现的技巧:输入字段值 - 特别是隐藏"的值- 与 URL 一起保存在浏览器的历史记录中 - 并且 - 当通过后退(或前进)按钮返回页面时调用 onLoad 事件.

For about a decade and half now, I have been using two tricks that I once discovered by painful trial and error: Input field values - especially 'hidden' ones - are preserved in the browser's history along with the URL - AND - the onLoad event is called when returning to the page by the back (or forward) buttons.

这意味着您可以根据需要存储尽可能多的状态" - 在隐藏字段中(记住将它们放在表单中),然后在onLoad"上重做"更改.我通常将渲染"部分作为一个单独的函数......换句话说,在动态发生时,我首先写入隐藏字段 - 然后调用渲染函数.然后,我将针对各种动态性的所有各种渲染函数聚集在一起,并从 onLoad 中调用它们.

This means that you can store as much 'state' as you like - in hidden fields (remember to put them in a form), and then 'redo' the changes on 'onLoad'. I usually make the 'render' part a separate function... In other words, at the time that the dynamicness is occurring, I first write to the hidden fields - then call the render function. Then I gather together all the various render functions for the various bits of dynamicness and call them from the onLoad.

我要强调的是,我从来没有在任何手册中寻找过这个 - 所以不能提供任何保证 - 但我一直在可靠地使用它一段时间(从 Netscape 开始!!!)它适用于许多"浏览器(所有的 IE、chrome、FF - 但至于其他的,我从未尝试过.)

I would stress that I have never gone hunting for this in any manuals - so cannot offer any assurances - but I have been using it reliably for a good while (since Netscape!!!) It works with 'many' browsers (all the IEs, chrome, FF - but as for the others, I've never tried.)

如果有人有更正确"且不那么乏味的方式,我会很高兴听到这个消息.但这似乎确实可以解决问题.

If anyone has a more 'correct' - and less tedious - way, I for one, will be very glad to hear about it. But this does seem to do the trick.

这篇关于在后退按钮上保留动态更改的 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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