如何控制离开页面上的浏览器确认对话框? [英] How to control browser confirmation dialog on leaving page?

查看:127
本文介绍了如何控制离开页面上的浏览器确认对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道与此相关的问题很多,但没有任何答案可以回答我.我想在用户离开页面时显示一个确认对话框.如果用户按取消",他将停留在页面上;如果确定",则将通过调用方法回滚其所做的更改.我这样做是这样的:

I know there are a lot of questions regarding this but nothing is answering me right. I want to show a confirmation dialog when user leaves the page. If the user press Cancel he will stay on page and if OK the changes that he has made will be rollback-ed by calling a method. I have done like this:

window.onbeforeunload = function () {
  var r = confirm( "Do you want to leave?" );
  if (r == true) {
       //I will call my method
  }
  else {
      return false;
  }
};

问题是我正在获取浏览器默认弹出窗口:"LeavePage/StayOnPage"

The problem is that I am getting the browser default popup: "LeavePage / StayOnPage"

此页面要求您确认要离开-您的数据 输入的内容可能无法保存.

This page is asking you to confirm that you want to leave - data you have entered may not be saved.

此消息显示在Firefox中,而在Chrome中则有所不同.在第一个确认对话框中按确定"后,我会看到此弹出窗口.

This message is shown in Firefox, in Chrome is a little different. I get this popup after I press OK on my first confirmation dialog.

有没有不显示此对话框的方法? (第二个,我没有创建). 或者,如果有任何方法可以控制此弹出窗口,是否有人知道该怎么做? 谢谢

Is there a way not to show this dialog? (the second one, that I did not create). Or if there is any way to control this popup, does anyone know how to do that? Thanks

推荐答案

这是我所做的,请进行修改以满足您的需求:

Here's what I've done, modify to fit your needs:

// This default onbeforeunload event
window.onbeforeunload = function(){
    return "Do you want to leave?"
}

// A jQuery event (I think), which is triggered after "onbeforeunload"
$(window).unload(function(){
    //I will call my method
});

注意:它已经过测试,可以在Google Chrome,IE8和IE10中使用.

Note: it's tested to work in Google Chrome, IE8 and IE10.

这篇关于如何控制离开页面上的浏览器确认对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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