当用户关闭浏览器时处理未保存的表单数据

发布于 2024-08-04 00:11:36 字数 474 浏览 7 评论 0原文

我很难弄清楚当浏览器关闭或用户更改页面时如何在表单中自动保存用户数据。当您想要打开对话框时,onBeforeUnload 事件 是可以的,但到那时保存更改就太晚了(除非您只是在 onBeforeUnload 处理程序中阻止浏览器)足够长的时间,足以将请求传递到服务器......但我宁愿不这样做)。

我确信你们中的一些人不得不处理未保存的表单问题。你做什么工作?你会:

  • 让用户丢失他们的更改
  • 使用模式窗口询问他们是否确定自己做了正确的事情,
  • 即时保存各个字段< /strong> 当它们发生变化时,
  • 或者您是否有某种终极方法可以在数据即将不可挽回地丢失时自动保存数据?

I'm having hard time trying to figure out how to auto-save user data in a form when the browser is being closed or user changes the page. The onBeforeUnload event is OK when you want to open a dialog box, but by then it's too late to save the changes (except if you just block the browser in the onBeforeUnload handler long enough for it to pass the request to the server...but I'd rather not do that).

I am sure some of you have had to deal with the unsaved form problem. What do you do? Do you:

  • let users just lose their changes,
  • ask them using a modal window if they are sure they did the right thing,
  • save individual fields on the fly as they change,
  • or do you have some ultimate method to automagically save the data when it's about to be lost irretrievably?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不一样的天空 2024-08-11 00:11:36

•使用模态窗口询问他们是否确定自己做了正确的事情,

关闭窗口是一种取消行为。由于用户从未主动提交表单,因此无法保证他们希望保存数据(可能不正确),并且您保存数据可能会给用户带来问题。

•ask them using a modal window if they are sure they do the right thing,

Closing a window is an act of cancellation. As the user never actively submitted the form, theres no guarantee that they want the data saved (it may not be correct), and you saving the data could cause problems for the user.

温柔戏命师 2024-08-11 00:11:36

我喜欢你的第三个选择:

  • 在各个字段发生变化时即时保存它们。

我不得不处理类似的情况,这就是我们正在做的事情。对我来说最重要的两点是:

  1. 改善用户体验 - 用户
    会对这样的形式印象深刻
    不会丢失更改。他们是
    一旦得到验证就“承诺”。
    例如,他输入了有效的电子邮件
    地址,并立即保存,
    此外,他还被提供了某种
    每个领域的反馈
    已成功保存(绿色
    例如,勾选,出现在旁边
    场)。
  2. 不再有“哦糟糕,我的浏览器崩溃了”
    我丢失了所有信息。'情况。

缺点:开发这样的解决方案需要额外的工时,而且它最终可能不会像更简单的解决方案那样降级。也就是说,在我看来,这仍然是值得的。

I like your third option:

  • save individual fields on the fly as they change.

I'm having to deal with a similar situation, and that's what we are doing. The two main things that sell that to me:

  1. Improved user experience - the user
    will be impressed by a form that
    does not lose changes. They are
    'committed' once they are validated.
    E.g., he types in a valid email
    address, and it is saved instantly,
    furthermore he is provided some sort
    of feedback for each field that is
    successfully been saved (a green
    tick for example, appears next to
    the field).
  2. No more 'oh crap my browser crashed
    and I lost all my info
    ' situations.

Disadvantages: The extra man-hours involved in developing such a solution, and the possibly that it ends up not degrading as nicely as a simpler solution. That said, it is still worth it IMO.

猫弦 2024-08-11 00:11:36

在我使用过的任何浏览器中,onBeforeUnload 都会为您提供一个模式窗口,要求用户确认是否要离开页面。您可以添加自己的文本,警告他们有未保存的数据,以帮助他们做出决定。您不想在没有用户请求的情况下显式保存,因为 a) 用户没有尝试保存,b) 如果您需要抛出任何验证错误,那就太晚了,因为页面已经在导航过程中离开。

In any browser I have used it in, onBeforeUnload provides you with a modal window which asks the user to confirm whether they want to leave the page or not. You can added your own text warning them that there is unsaved data, to help them decide. You don't want to explicitly save without the user's request, because a) the user did not attempt to save, and b) if you need to throw any validation errors it will be too late as the page is already in the process of navigating away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文