onbeforeunload 确认屏幕自定义

发布于 2024-08-03 10:40:10 字数 457 浏览 5 评论 0原文

是否可以在浏览器中为 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?';
    }
}

我正在使用 jQuery 来实现此目的。

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).

At the moment my code looks like:

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?';
    }
}

I'm using jQuery for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自演自醉 2024-08-10 10:40:10
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;
};

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

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.

恰似旧人归 2024-08-10 10:40:10

不,您无法避免浏览器中的标准版本。您所能做的就是向其中注入一些自定义文本;如果您使用以下事件处理程序(注册原型库方式):(

Event.observe(window, "beforeunload", function(event) {
    if (showMyBeforeUnloadConfirmation)
        event.returnValue = "foo bar baz";
});

并且 showMyBeforeUnloadConfirmation 为 true),您将获得浏览器的标准确认,其中包含以下文本:

您确定要离开此页面吗?

foo 酒吧巴兹

按“确定”继续,或按“取消”留在当前页面。

[确定][取消]

No, you can't avoid the standard one from the browser. All you can do is inject some custom text into it; if you use the following event handler (registered the prototype lib way):

Event.observe(window, "beforeunload", function(event) {
    if (showMyBeforeUnloadConfirmation)
        event.returnValue = "foo bar baz";
});

(and showMyBeforeUnloadConfirmation is true) you'll get the browser's standard confirmation with the following text:

Are you sure you want to navigate away from this page?

foo bar baz

Press OK to continue, or Cancel to stay on the current page.

[ OK ] [ Cancel ]

南风几经秋 2024-08-10 10:40:10

我遇到了同样的问题,我能够获得带有消息的自己的对话框,但我面临的问题是:

  1. 它在所有导航上给出消息,而我只需要关闭单击即可。
  2. 使用我自己的确认消息,如果用户选择“取消”,它仍然显示浏览器的默认对话框。

以下是我找到的解决方案代码,我将其写在我的母版页上。

function closeMe(evt) {
    if (typeof evt == 'undefined') {
        evt = window.event;
    }
    if (evt && evt.clientX >= (window.event.screenX - 150) && evt.clientY >= -150 && evt.clientY <= 0) {
        return "Do you want to log out of your current session?";
    }
}

window.onbeforeunload = closeMe;

I faced the same problem, I was able to get its own dialog box with my message, but the problems I faced were:

  1. It was giving message on all navigations and I wanted it only for close click.
  2. With my own confirmation message if user selects "Cancel", it still shows the browser's default dialog box.

Following is the solutions code I found, which I wrote on my Master page.

function closeMe(evt) {
    if (typeof evt == 'undefined') {
        evt = window.event;
    }
    if (evt && evt.clientX >= (window.event.screenX - 150) && evt.clientY >= -150 && evt.clientY <= 0) {
        return "Do you want to log out of your current session?";
    }
}

window.onbeforeunload = closeMe;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文