【问题标题】:Stop page from redirecting after msgbox is showed显示 msgbox 后停止页面重定向
【发布时间】:2017-01-07 20:21:54
【问题描述】:

我希望页面在显示 msgBox 时停止重定向,并等待用户按下框上的按钮。

我可以在页面卸载时显示 msgbox,但页面仍会重定向到另一个网页,而无需等待用户输入。

<script>
    $(window).bind('beforeunload', function () {
        msgbox();
    });

    function msgbox() {

        $.msgBox({
            title: "Confirmation",
            content: "Do you agree with the information provided?",
            type: "confirm",
            buttons: [{ value: "Yes" }, { value: "No" }],
            success: function (result) {
                if (result == "Yes") {
                    window.location.replace("somepage/controller/func2");
                } else {
                    window.location.replace("somepage/controller/func1");
                }
            }
        });
    }
</script>

【问题讨论】:

    标签: jquery msgbox


    【解决方案1】:

    可能是触发 msgbox 时设置了一些重定向?

    试试这个:

    $(window).bind('beforeunload', function () {
        msgbox();
        return false;
    });
    

    添加Return false 将阻止任何进一步的处理发生,并将进一步执行的控制权交给msgBox。

    【讨论】:

    • 但现在它会显示一条警报“您所做的更改可能无法保存”,其中包含离开和停留选项。点击stay后会显示msgbox。
    • @SahibUzZaman 好吧,在 'beforeunload' 事件中是无法避免的。出于安全原因和默认设置。查看此SO answer 了解更多详情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    相关资源
    最近更新 更多