如何在用户点击一个回浏览器按钮,显示确认? [英] How to display confirmation if user clicks on a back browser button?

查看:372
本文介绍了如何在用户点击一个回浏览器按钮,显示确认?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/136937/is-there-a-way-to-catch-the-back-button-event-in-javascript\">Is有没有办法赶上后退按钮事件在JavaScript?

在我的应用程序只需要一定数量的脚本(Text1.php,Text2.php和Text3.php)的含有一块code在哪里,如果用户点击后浏览器按钮,它会显示类似下面的确认对话框:

In my application I want only a certain number of scripts (Text1.php, Text2.php and Text3.php) to contain a piece of code where that if the user clicks on the back browser button, it will display a confirmation box like the one below:

confirm("You have clicked on the browser's back button. If you confirm to go back via the browser back button, you will be logged out and lose your current assessment details." + "\n" + "\n" + "Are you sure you want to go back and lose current assessment details?" + "\n" );

的质量要求如下:

The requirments are the following:


  1. 如何显示的确认,如果用户点击后浏览器按钮?

  2. 如果用户确认再确认导航到Text4.php(这是它注销用户)

  3. 如果用户取消确认然后就关闭确认对话框,以便用户可以继续他们在做什么(猜这是与返回false;

  4. 只希望这种事情发生的脚本(Text1.php,Text2.php和Text3.php)

  5. 需要它在所有主要的浏览器(IE,Chrome浏览器,火狐,Safari,Opera)的工作

感谢您

推荐答案

注意:您应该只有做到这一点,如果你需要prevent离开的人(也就是,如果他们'已经输入的一些重要数据,并通过导航客场失去它)。还要注意的是,你不能自定义按钮或标题的只有的消息主体本身。这些是这样的功能的限制。

Note: You should ONLY do this if you NEED to prevent the person from leaving (that is, if they've inputted some important data and will lose it by navigating away). Also note that you cannot customize the buttons or title, only the message body itself. These are the restrictions of such a feature.

您可以使用 beforeunload 事件来实现这一目标。

You can use the beforeunload event to achieve this.

$(window).bind("beforeunload", function() {
    return "HI"; // Return whatever message you want
});​

小提琴

(要使用的小提琴,只需重新加载页面或离开或诸如此类的东西。)

我也建议你解除这个人后离开页面所需的方式(一个取消按钮?),或者您可能会遇到不良行为。

I would also suggest you unbind this upon someone leaving the page in the desired way (a cancel button?), or you may run into undesirable behavior.

例如:

$("a").click(function() {
    $(window).unbind("beforeunload");
});

这篇关于如何在用户点击一个回浏览器按钮,显示确认?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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