onbeforeunload确认屏幕定制 [英] onbeforeunload confirmation screen customization

查看:118
本文介绍了onbeforeunload确认屏幕定制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在浏览器中为onbeforeunload事件创建自定义确认框?我尝试了但是后来我得到了2个确认框(一个来自我,这只不过是返回确认...然后是浏览器中的标准框)。

Is it possible to create a custom confirmation box for the onbeforeunload event in a browser? I tried but then I get 2 confirmation boxes (one from me which is nothing more than return confirm... and then the standard one from the browser).

我的代码看起来像:

var inputChanged = false;

$(window).load(function() {
    window.onbeforeunload = navigateAway;
    $(':input').bind('change', function() { inputChanged = true; });
});

function navigateAway(){
    if(inputChanged){
        return 'Are you sure you want to navigate away?';
    }
}

我正在使用jQuery。

I'm using jQuery for this.

推荐答案

window.onbeforeunload = function (e) {
  var message = "Your confirmation message goes here.",
  e = e || window.event;
  // For IE and Firefox
  if (e) {
    e.returnValue = message;
  }

  // For Safari
  return message;
};

请注意:大多数浏览器会将此消息放在其他文本之后。您无法完全控制确认对话框的内容。

Please note: Most browsers put this message after some other text. You do not have complete control of the content of the confirmation dialog.

这篇关于onbeforeunload确认屏幕定制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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