如何检测如果用户一定要使用后退按钮的网页? [英] How do I detect if a user has got to a page using the back button?

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

问题描述

这个问题类似于<一href="http://stackoverflow.com/questions/55871/track-when-user-hits-back-button-on-the-browser">http://stackoverflow.com/questions/55871/track-when-user-hits-back-button-on-the-browser,但不一样的...我有一个解决方案,我张贴在这里,以供参考和反馈。如果任何人有任何更好的选择,我所有的耳朵!

This question is similar to http://stackoverflow.com/questions/55871/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!

的情况是,我有一个页面,就地编辑,一拉的Flickr。即有一个点击此处添加说明DIV,这时候点击轮流与保存一个textarea和取消按钮。点击保存职位数据到服务器来更新数据库,并提出了新的描述在DIV来代替文字区域。如果页面被刷新,新的描述从数据库中显示一个点击编辑选项。相当标准的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?

推荐答案

使用隐藏的表单。表单数据在浏览器preserved(典型值),当您重新加载或打后退按钮返回到一个页面。在你的页面下面去(可能是底部附近):

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';
}

这嗅着形式有执行后的HTML完全解析,但你可以把双方的形式和内联JS在页面(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天全站免登陆