onbeforeunload确认屏幕定制

Nyl*_*ska 6 javascript confirm onbeforeunload

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

目前我的代码如下:

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?';
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用jQuery.

Eli*_*rey 11

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;
};
Run Code Online (Sandbox Code Playgroud)

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