0

I want the page to stop from redirecting when the msgBox is displayed and wait untill user has pressed a button on the box.

I am able to display the msgbox when the page unloads, but still the page redirects to another webpage without waiting for user's input.

<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>
harkirat1892
  • 453
  • 5
  • 19

1 Answers1

0

May be some redirection is set when msgbox is triggered?

Try this:

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

Adding Return false will prevent any further processing from happening, and give the control about further executions to msgBox.

harkirat1892
  • 453
  • 5
  • 19
  • But now it shows an alert "Changes you made may not be saved" with Leave and Stay options. After clicking stay it shows the msgbox. – Sahib Uz Zaman Jan 09 '17 at 07:29
  • @SahibUzZaman Well, that can not be avoided on 'beforeunload' event. It is there for security reasons, and the default. Check out this [SO answer](http://stackoverflow.com/a/32018719/2588459) for more details. – harkirat1892 Jan 10 '17 at 09:33