如何检测用户是否使用后退按钮访问了某个页面? [英] How do I detect if a user has got to a page using the back button?

查看:24
本文介绍了如何检测用户是否使用后退按钮访问了某个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题类似于跟踪用户何时点击返回按钮在浏览器上,但不一样...我有一个解决方案,并在此处发布以供参考和反馈.如果有人有更好的选择,我会全力以赴!

This question is similar to Track when user hits back button on the browser, but not the same... I have a solution and am posting it here for reference and feedback. If anyone has any better options, I'm all ears!

情况是我有一个带有就地编辑"的页面,a la flickr.IE.有一个单击此处添加描述"DIV,单击时会变成带有保存"和取消"按钮的 TEXTAREA.单击保存"将数据发布到服务器以更新数据库,并将新描述放在 DIV 中代替 TEXTAREA.如果刷新页面,则会从数据库中显示新的描述,并带有单击以编辑"选项.这些天相当标准的 Web 2.0 东西.

The situation is that I have a page with an "in place edit", a la flickr. I.e. there is a "click here to add a description" DIV, which when clicked turns into a TEXTAREA with Save and Cancel buttons. Clicking Save posts the data to the server to update the database and puts the new description in the DIV in place of the TEXTAREA. If the page is refreshed, the new description is displayed from the database with a "click to edit" option. Fairly standard web 2.0 stuff these days.

问题是如果:

  1. 页面加载时没有描述
  2. 用户添加了描述
  3. 通过点击链接导航离开该页面
  4. 用户点击后退按钮

然后显示的内容(来自浏览器的缓存)是没有包含新描述的动态修改 DIV 的页面版本.

Then what is displayed (from the browser's cache) is the version of the page without the dynamically modified DIV containing the new description.

这是一个相当大的问题,因为用户认为他们的更新已经丢失,并且不一定明白他们需要刷新页面才能看到更改.

This is a fairly big problem as the user assumes that their update has been lost and won't necessarily understand that they need to refresh the page to see the changes.

那么,问题是:如何在页面加载后将页面标记为正在修改,然后检测用户何时返回该页面"并在这种情况下强制刷新?

So, the question is: How can you flag a page as being modified after it has loaded, and then detect when the user "goes back to it" and force a refresh in that situation?

推荐答案

使用隐藏表单.当您重新加载或点击后退按钮返回页面时,表单数据(通常)会保留在浏览器中.您的页面中包含以下内容(可能靠近底部):

Use a hidden form. Form data is preserved (typically) in browsers when you reload or hit the back button to return to a page. The following goes in your page (probably near the bottom):

<form name="ignore_me">
    <input type="hidden" id="page_is_dirty" name="page_is_dirty" value="0" />
</form>

在您的 javascript 中,您将需要以下内容:

In your javascript, you will need the following:

var dirty_bit = document.getElementById('page_is_dirty');
if (dirty_bit.value == '1') window.location.reload();
function mark_page_dirty() {
    dirty_bit.value = '1';
}

嗅探表单的 js 必须在 html 完全解析后执行,但如果用户延迟是一个严重问题,您可以将表单和 js 内联放在页面顶部(js 秒).

The js that sniffs the form has to execute after the html is fully parsed, but you could put both the form and the js inline at the top of the page (js second) if user latency is a serious concern.

这篇关于如何检测用户是否使用后退按钮访问了某个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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