javascript - Capture the result of window.onbeforeunload - Stack Overflow

I have a scenario, I have to save the changes when user clicks yes on window.onbeforeunload for that I

I have a scenario, I have to save the changes when user clicks yes on window.onbeforeunload for that I need to submit the form and nothing should be happen when selected no.

Any help is greatly appreciated.

I have tried this:

window.onbeforeunload= function(){
    var r = confirm("Are you sure you want to leave this page?");
    if (r == true) {
        readForm.action = '/SREPS/read.do' ;
        readForm.submit();
    }else{
        return false;
    }
}

It did not worked 100% when ever we hit Cancel during window.confirm another dialog appears saying that message from web page false asking for confirmation leave this page and stay on this page. In this case if the user selects leave this page. I am not able to submit the form.

I have a scenario, I have to save the changes when user clicks yes on window.onbeforeunload for that I need to submit the form and nothing should be happen when selected no.

Any help is greatly appreciated.

I have tried this:

window.onbeforeunload= function(){
    var r = confirm("Are you sure you want to leave this page?");
    if (r == true) {
        readForm.action = '/SREPS/read.do' ;
        readForm.submit();
    }else{
        return false;
    }
}

It did not worked 100% when ever we hit Cancel during window.confirm another dialog appears saying that message from web page false asking for confirmation leave this page and stay on this page. In this case if the user selects leave this page. I am not able to submit the form.

Share Improve this question edited Sep 23, 2022 at 17:59 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 7, 2014 at 15:56 HarishHarish 4553 gold badges7 silver badges21 bronze badges 3
  • 1 possible duplicate of How to capture browser close event and make a request to web service method on that event through javascript – Denys Séguret Commented May 7, 2014 at 15:57
  • I have posted my answer below – Harish Commented May 7, 2014 at 16:36
  • @Harish: Don't post it as an answer if it does not answer/solve the question. – gen_Eric Commented May 7, 2014 at 20:04
Add a ment  | 

1 Answer 1

Reset to default 6

You cannot use your own dialog in onbeforeunload. The only thing you can do is return a string to be displayed (on some browsers). You cannot stop the browser from leaving, only the user can control that.

What you can do is the following:

window.onbeforeunload = function(){
    return 'Are you sure you want to leave this page?';
};

This will ask the user if they want to leave or not. Then you can use the onunload event to run a function when they leave. From there, you can make a "synchronous" AJAX request to submit the form.

window.onunload = function(){
    var request = new XMLHttpRequest();
    request.open('POST', '/SREPS/read.do', false);
    request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

    request.send(new FormData(readForm));
};

If you are using jQuery, you can do:

window.onunload = function(){
    $.ajax({
        url: '/SREPS/read.do',
        type: 'post',
        async: false,
        data: $(readForm).serialize()
    });
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745159657a4614332.html

相关推荐

  • javascript - Capture the result of window.onbeforeunload - Stack Overflow

    I have a scenario, I have to save the changes when user clicks yes on window.onbeforeunload for that I

    9小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信