如果他真的想离开页面,我如何要求网络用户确认? [英] How can I ask a web user for confirmation if he really wants to leave the page?

查看:98
本文介绍了如果他真的想离开页面,我如何要求网络用户确认?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何询问用户您确定要离开页面吗?

例如,如果单击后退按钮在Stackoverflow上问一个问题?

Like for example if you click the back button while asking a question on Stackoverflow?

推荐答案

最简单的方法是将事件处理程序绑定到卸载JavaScript事件。 jQuery使用 .unload()事件处理程序非常容易。在绑定的方法中,您可以检查页面的表单字段是否有文本输入。假设他们确实弹出警告通知用户,如果他们从页面导航,他们将丢失任何未保存的数据。

The easiest way to do this is to bind an event handler to the "unload" JavaScript event. jQuery makes this very easy to do with its .unload() event handler. In the method you bind you can check to see if any the page's form fields have text input. Assuming they do pop an alert notifying the user they'll lose any unsaved data if they navigate from the page.

此方法将在用户离开时触发警报任何原因的页面。

This method will fire an alert whenever the user navigates away from the page for any reason.

$(window).bind('beforeunload', function() {
  alert('Handler for .beforeunload() called.');
});

这显然不是非常用户友好,但一些快速修改可以使你的问题变得可行。

That's obviously not very user friendly but a couple of quick modifications can make it workable to your question.

这篇关于如果他真的想离开页面,我如何要求网络用户确认?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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