且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在离开页面之前询问用户确认的***方式

更新时间:2023-02-06 18:40:59

当我看到阻止用户这个词时,我开始痛苦地哭泣。 从不 阻止用户,只能帮助他们。

As soon as I saw the words "prevent the user" I started to wail in agony. Never prevent the user, only help them.

如果他们看到您的注册页面并且已经开始运行,那是他们的选择。如果他们已经填写了一些数据(因为他们可能会意外导航),请弹出一个javascript确认框,但保留它。如果他们没有触及表单,请不要管它们 - 他们不希望填写您的表单。

If they see your registration page and run off, that's their choice. Pop up a javascript confirm box if they've already filled in some data (because they might be navigating away accidentally) but leave it at that. If they haven't touched the form, leave them alone - they don't want to fill in your form.

查看其他方法吸引用户如果您的表单庞大而且可怕,请将其分解为简单易管理的块或更好的,简化操作以至于用户只在您需要时为您提供数据。例如,在您想要向他们发布内容之前,您可能不需要他们的地址。

Look at other methods of engaging users. If your form is huge and scary, break it into simple manageable chunks or better yet, simplify things so much that the user only gives you data when you need it. For example, you might not need their address until you want to post something to them.

通过将其分成多个部分,您可以使用简单的表单将它们挂钩他们投入了那段时间,他们更有可能继续这个过程。

By breaking it into multiple parts you can hook them with a simple form and once they've invested that time, they'll be more likely to continue the process.

但不要骚扰用户。如果他们不想注册,那么用弹出窗口和jaavscript对话来纠缠他们就会完全将他们赶出网站。

考虑到这一点,假设你只是试图阻止人们填写半填表格,有几种方法可以真正帮助别人:

With that in mind, assuming you're just trying to stop people half-filling-in forms, there are a couple of options to genuinely help people:


  1. 检测表单是否已更改并向他们询问简单的 confirm()消息。

这是全部你可以做。 CSSpop-in无法正常工作,因为你无法在卸载事件中控制*窗口位置。

This is all you can do. A CSS "pop-in" just won't work because you can't control* the window location in the unload event.

*你可以在你的所有页面的链接上放置一个事件监听器来触发检查表单,但这只有在用户点击其中一个链接时才有用。例如,如果用户单击或关闭窗口,则无济于事。你可以同时执行CSS和javascript,但最终会有点混乱。

*You can put an event listener on all your page's links to fire off something to check the form, but this only helps if the user clicks on one of those links. It won't help if, for example, the user clicks back or closes the window. You could do both CSS and javascript but you end up with a bit of a mess.

坚持后面的表单状态场景。

Persist the state of the form behind the scenes.

#1的扩展。而不是与用户争吵,让他们去他们想要的地方,但将表格的内容保存到会话或cookie(如果它适合)并在页面上放一些东西(如页面顶部的SO的橙色提示栏) )提醒他们他们已经开始填写表格并给他们一个回到表格的链接。

An extension to #1. Instead of squabbling with the user, let them go where they want but save the content of the form either to session or cookie (if it'll fit) and put something on the page (like SO's orange prompt bars at the top of the page) that reminds them that they've started filling in a form and gives them a link back to the form.

当他们点击该链接时,你加载数据将cookie(或会话)重新打包到表单中并让它们继续运行。这有明显的好处,让他们在您的网站上做他们喜欢的事情,并保持数据的安全..是。

When they click that link, you load the data out of the cookie (or session) back into the form and let them carry on. This has the clear benefit of letting them do what they like on your site and keeps the data safe.. ish.

如果他们没有回来并且他们的cookie /会话到期,那就是他们的错。你只能把马带到水里。强迫它喝酒不是你的工作。

If they don't come back and their cookie/session expire, that's their fault. You can only lead a horse to water. It's not your job to force it to drink.