beforeunload 窗口显示两个对话框

发布于 2024-09-13 14:26:49 字数 909 浏览 3 评论 0原文

我有这个 jQuery 代码;

$(function () {
    $(window).unbind("beforeunload");
    $(window).bind("beforeunload", function () {
        return confirm("Really?");
    });
});

当我关闭窗口时,我会收到确认请求,如果我点击“取消”,我会收到第二次确认,其中显示:

“您确定要离开此页面吗?”

“假”

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

为什么我会出现第二个对话框?有办法将其删除吗?

编辑

已将代码更改为;

$(function () {
    $(window).bind("beforeunload", function () {
        return "slappy?";
    });
});

但没有出现确认消息。该事件正在触发,因为我可以在其中放置警报并查看警报。

编辑2

已将代码更改为:

window.onbeforeunload = function () {
    var txtBlog = $('#tbxNote').val();
    if (txtBlog != "")
        return "You have not saved your blog entry.";
}

它有效,但我的消息上方和下方还有其他文字;

“您确定要离开此页面吗?”

“您尚未保存您的博客条目。”

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

I have this jQuery code;

$(function () {
    $(window).unbind("beforeunload");
    $(window).bind("beforeunload", function () {
        return confirm("Really?");
    });
});

When i close my window I get the confirmation request and if I hit "Cancel" I get a second confirmation which says;

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

"False"

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

Why am I getting a second dialog and is there a way to remove it?

edit

have changed the code to be;

$(function () {
    $(window).bind("beforeunload", function () {
        return "slappy?";
    });
});

But the confirmation message does not appear. The event is firing because I can put an alert in there and see the alert.

edit 2

Have changed the code to this;

window.onbeforeunload = function () {
    var txtBlog = $('#tbxNote').val();
    if (txtBlog != "")
        return "You have not saved your blog entry.";
}

it works but there is other text above and below my message;

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

"You have not saved your blog entry."

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

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

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

发布评论

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

评论(1

∞觅青森が 2024-09-20 14:26:49

只需使用:

return "Really?"; 

beforeunload 事件要求您返回一个字符串,其中包含您想要在标准“您确定”对话框中显示的消息。

Just use:

return "Really?"; 

The beforeunload event requires you to return a string containing the message you want displayed in the standard "Are you sure" dialog.

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