保留表单数据的策略(在选项卡/浏览器关闭时)

发布于 2024-08-08 10:15:59 字数 550 浏览 14 评论 0原文

我的任务管理应用程序有一个问题,用户偶尔会关闭浏览器/选项卡,并且他们输入的信息会消失,因为他们不小心关闭了浏览器/选项卡,导致他们输入的文本丢失(有些可以花半个小时输入文字)。

所以我必须提供一个解决方案,我有几个想法,但希望了解最好的解决方案,或者如果您有更好的解决方案,请告诉我。

选项 1:

  • window.onunload 或可能的 window.onbeforeunload 事件上调用一个确认()对话框,并首先测试任务记录区域中是否有任何文本,并且没有空白的。如果不为空,则调用 window.confirm() 并询问用户是否要关闭选项卡/窗口而不保存日志。

我对选项 #1 的担忧是它可能会干扰用户。

选项 2:

  • 在同一事件中,不调用任何确认(),而是将任务日志区域中的文本强制保存在 cookie 中。然后可能会提供一个按钮,尝试从同一页面上的 cookie 恢复任何已保存的任务信息,因此点击该按钮将使其解析 cookie 并检索信息。

I have an issue with a task management application where occasionally users close their browsers/tabs and the information which they type goes away because they accidentally close a browser/tab, resulting in the loss of the text which they've entered ( and some can spend half an hour entering in text ).

So I have to provide a solution, I have a couple ideas but wanted input on the best to go with, or if you have a better solution let me hear ya.

Option 1:

  • On the window.onunload or possibly window.onbeforeunload event invoke a confirm() dialog and first test whether the task logging area has any text in it and is not blank. If it's not blank, invoke window.confirm() and ask whether the user wants to close the tab/window without saving a log.

My concern with option #1 is that it may be user intrusive.

Option 2:

  • On the same event, don't invoke any confirm() but instead forcefully save the text in the task logging area in a cookie. Then possibly offer a button that tries to restore any saved task information from the cookie on the same page, so hitting that button would make it parse the cookies and retrieve the information.

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

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

发布评论

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

评论(3

渡你暖光 2024-08-15 10:15:59

window.onbeforeunload 事件的工作方式有点奇怪。如果您为其定义一个处理程序,浏览器将显示一条有关因离开页面而丢失数据的通用消息,并将从处​​理程序函数返回的字符串插入到消息中间。请参阅此处:

替代文本 http://img291.imageshack.us/img291/8724/windowonbeforeunload .png

那么我们要做的:当我们知道页面上的某些内容未保存时,我们设置:

window.onbeforeunload = function(){
 return "[SOME CUSTOM MESSAGE FROM THE APP]";
}

一旦用户保存,并且我们知道我们不需要向他们显示消息,我们设置

window.onbeforeunload = null;

:干扰不大,但比用户意外丢失数据要好。

The window.onbeforeunload event works a little strangely. If you define a handler for it, the browser will display a generic message about losing data by navigating away from the page, with the string you return from the handler function inserted into the middle of the message. See here:

alt text http://img291.imageshack.us/img291/8724/windowonbeforeunload.png

So what we do: when we know something on the page is unsaved, we set:

window.onbeforeunload = function(){
 return "[SOME CUSTOM MESSAGE FROM THE APP]";
}

and once the user saves, and we know we don't need to show them the message, we set:

window.onbeforeunload = null;

It is a little intrusive, but it's better than your users losing data accidentally.

嘴硬脾气大 2024-08-15 10:15:59

如果用户足够愚蠢,在提交他们一直在做的事情之前就离开了,那么他们不应该介意有人闯入,询问他们是否打算做一些明显愚蠢的事情。

此外,SO 在离开时使用确认对话框,这里的大多数(某些)用户都非常聪明。

这是最容易使用的,并且可能会给用户带来更多帮助。

如果有人写了一段很长的文本,然后关闭浏览器而不提交它,他们可能会更高兴在那里解决问题,而不是第二天早上发现他们没有这样做......

If the user is daft enough to navigate away before submitting what they have been doing, then they shouldn't mind an intrusion to ask if they mean to do something that is apparently stupid.

Also, SO uses a confirmation dialog on navigating away, and most (some) users here are pretty smart.

This is the easiest to use, and will probably help the users more.

If someone writes a long piece of text, then closes the browser without submitting it, they might be more pleased to sort the problem there and then rather than finding out the next morning they didn't do it...

若水般的淡然安静女子 2024-08-15 10:15:59

我会研究您正在使用的特定 Web 服务器/语言的 AJAX 框架。 AJAX 允许您在键入时保存表单数据(例如,这就是 Google Docs 的工作原理)。

I would research AJAX frameworks for the particular web server/languages you are using. AJAX would allow you to save form data as it is typed (for example, this is how Google Docs works).

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